在PHP面向对象编程中,
$a = &$b
,$a = $b
和$b = clone $a
有什么区别?其中$a
是一个类的实例。$a = &$b
,$a = $b
和$b = clone $a
有什么区别?其中$a
是一个类的实例。// $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;
// $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;
$a = $b
会让它们指向同一个对象,但对于其他类型,当你执行$a = $b
时,改变$a
的值不会影响$b
。 - xdazz