2012-06-25 170 views
3

PHP OOP中的$a = &$b,$a = $b$b = clone $a之间的区别是什么? $a是一个类的实例。

回答

8
// $a is a reference of $b, if $a changes, so does $b.  
$a = &$b; 

// assign $b to $a, the most basic assign. 
$a = $b; 

// This is for object clone. Assign a copy of object `$b` to `$a`. 
// Without clone, $a and $b has same object id, which means they are pointing to same object. 
$a = clone $b; 

并检查与ReferencesObject Cloning更多信息。

+1

+1比我快:) –

+0

我写的也差不多了! +1,但我希望你能更多地解释PHP的引用和克隆。更新:当然你更新了你的答案,同时我发布了评论:D – Adi

+0

我不明白$ a = $ b之间的主要区别是什么;和$ a = &$b;如果你看看这里的第一个例子http://php.net/manual/en/language.oop5.references.php它给出了相同的结果 –

0
// $a has same object id as $b. if u set $b = NULL, $a would be still an object 
$a = $b; 

// $a is a link to $b. if u set $b = NULL, $a would also become NULL 
$a = &$b; 

// clone $b and store to $a. also __clone method of $b will be executed 
$a = clone $b; 
-1

如果你不知道什么是ZVAL结构,什么是引用计数,is_ref在ZVAL结构有关,只是需要一些时间PHP's garbage collection

相关问题