是什么区别$a = &$b
, $a = $b
和$b = clone $a
在PHP OOP? $a
是一个类的实例。
Answer 1:
// $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;
并检查使用的详细信息参考资料 , 对象克隆 。
Answer 2:
// $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;
Answer 3:
如果你不知道什么是ZVAL结构,什么是引用计数,is_ref在ZVAL结构有关,只是需要一些时间PHP的垃圾回收 。
文章来源: Difference between $a=&$b , $a = $b and $a= clone $b in PHP OOP