If objects are passed by reference in PHP5, then why $foo below doesn’t change?
$foo = array(1, 2, 3);
$foo = (object)$foo;
$x = $foo; // $x = &$foo makes $foo (5)!
$x = (object)array(5);
print_r($foo); // still 1,2,3
so:
Passing by reference not the same as
assign.
then why $foo below is (100, 2, 3) ?
$foo = array('xxx' => 1, 'yyy' => 2, 'zzz' => 3);
$foo = (object)$foo;
$x = $foo;
$x->xxx = 100;
print_r($foo);
The problem lies here:
On the first rule $x is referenced to $foo; editing $x wil also edit $foo;
(this is called “assign by reference”, not “pass by reference” *1)
Will cause $foo to also have a property “myProperty”.
But on the next line you reference $x to a
newobject.Effectively unreferencing $x from $foo, all changes you make to $x won’t propogate to $foo.
*1: When you call a function, the objects you pass to the functions are (in php5) “passed by reference”