美元与美元之间的差额=&$PHP OOP中的b、$a=$b和$a=克隆$b
在PHP OOP中,美元与美元之间的差额=&$PHP OOP中的b、$a=$b和$a=克隆$b,php,class,reference,clone,copy-initialization,Php,Class,Reference,Clone,Copy Initialization,在PHP OOP中,$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`. // Wi
$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;
如果你不知道什么是ZVAL结构,什么是refcount,ZVAL结构中的ref是关于什么的,那就花点时间吧。我也写了几乎相同的东西+1,尽管我希望您对PHP引用和克隆进行更多的解释。更新:当然,你在我发布评论的同时更新了你的答案:我不清楚$a=$b之间的主要区别是什么;和$a=&$b;如果你看这里的第一个例子,它给出了相同的结果result@AnonimWd是的,对于对象,因为使用
$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;