看来PHP对象是通过引用传递的.甚至赋值运算符似乎也没有创建Object的副本.
这是一个简单,人为的证明:
<?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'
?>
Run Code Online (Sandbox Code Playgroud)
在两个印刷案例中,我都在"追求"
那么,我如何通过值传递$ a到set_b(),而不是通过引用?