2012-06-25 167 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

相關問題