P粉0381618732023-08-25 09:56:52
To copy an object you need to use Object Clone一个>.
To do this in your example:
$x = clone $obj;
Note that objects can define their own clone
behavior using __clone()
, which may give you unexpected behavior, so please keep this in mind.
P粉7138468792023-08-25 00:31:47
<?php $x = clone($obj);
So it should be like this:
<?php 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 = clone($obj); print_r($x) refObj($obj); // $obj is passed by reference print_r($x)