【发布时间】:2012-06-26 10:29:57
【问题描述】:
PHP OOP 中的$a = &$b、$a = $b 和$b = clone $a 有什么区别? $a 是一个类的实例。
【问题讨论】:
标签: php class reference clone copy-initialization
PHP OOP 中的$a = &$b、$a = $b 和$b = clone $a 有什么区别? $a 是一个类的实例。
【问题讨论】:
标签: php class reference clone copy-initialization
// $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;
通过References、Object Cloning查看更多信息。
【讨论】:
$a = $b 会让它们指向同一个对象,但是对于其他类型,当你做$a = $b 时,改变$a 的值不会影响$b.
// $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;
【讨论】:
如果你不知道什么是ZVAL结构,什么是refcount,ZVAL结构中的is_ref是关于什么的,请花点时间了解PHP's garbage collection。
【讨论】: