PHP OOP中$ a =& $ b,$ a = $ b和$ a = clone $ b之间的差异

时间:2012-06-25 07:00:23

标签: php class reference clone copy-initialization

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

3 个答案:

答案 0 :(得分: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 :(得分: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;

答案 2 :(得分:-1)

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