在PHP中,对象似乎是通过引用传递的。即使是赋值操作符也不会创建对象的副本。
这里有一个简单的,做作的证明:
<?php
class A {
public $b;
}
function set_b($obj) { $obj->b = "after"; }
$a = new A();
$a->b = "before";
$c = $a; //i would especially expect this to create a copy.
set_b($a);
print $a->b; //i would expect this to show 'before'
print $c->b; //i would ESPECIALLY expect this to show 'before'
?>
在两种打印情况下,我都得到了“ After ”
那么,如何通过值而不是引用将A美元传递给设置_B()呢?