【发布时间】:2011-05-06 15:10:20
【问题描述】:
默认情况下 PHP5 OOP objects are passed by reference 是有据可查的。如果这是默认情况下,在我看来,没有默认的复制方式没有参考,如何??
function refObj($object){
foreach($object as &$o){
$o = 'this will change to ' . $o;
}
return $object;
}
$obj = new StdClass;
$obj->x = 'x';
$obj->y = 'y';
$x = $obj;
print_r($x)
// object(stdClass)#1 (3) {
// ["x"]=> string(1) "x"
// ["y"]=> string(1) "y"
// }
// $obj = refObj($obj); // no need to do this because
refObj($obj); // $obj is passed by reference
print_r($x)
// object(stdClass)#1 (3) {
// ["x"]=> string(1) "this will change to x"
// ["y"]=> string(1) "this will change to y"
// }
此时我希望$x 成为原始$obj,但当然不是。有什么简单的方法可以做到这一点还是我必须编写一些代码like this
【问题讨论】:
标签: php object pass-by-reference