r/PHPhelp • u/GigfranGwaedlyd • Apr 13 '25
How variables assigned by reference to array elements can cause unexpected results
Just when I thought I had a good grasp on references in PHP after all these years, along comes this example code I just happened to read on php.net that nearly made my head explode:
/* Assignment of array variables */
$arr = array(1);
$a =& $arr[0]; // $a and $arr[0] are in the same reference set
$arr2 = $arr; // Not an assignment-by-reference!
$arr2[0]++;
/* $a == 2, $arr == array(2) */
/* The contents of $arr are changed even though it's not a reference! */
WTF?! If $arr2 is not an assignment-by-reference, then how is $arr2[0] acting as a reference to $arr[0]??? Just because $a is set as a reference to $arr[0]? It seems that assigning $a as a reference to $arr[0] changes $arr[0] into a reference as well, and so when $arr is assigned to $arr2 (even though the assignment is NOT by reference) then $arr2[0] references the same thing as well. But this only happens when assigning the entirety of $arr to $arr2, and only if there is an already-existing reference to $arr[0]. If you were to assign another variable, $b, to $arr[0], and not do the assignment by reference, $b would not be a reference. That makes sense since $arr[0] is a scalar value (or, I guess, a REFERENCE to a scalar value, anyway). It would be no different than if you did $a = 5; $b = &$a; $c = $b; $c would NOT be a reference to $a.
All of this is to say that I SORT OF understand what's going on, but not completely. By the way, the same behavior happens with objects even if you clone another object:
$obj = new stdClass();
$obj->foo = 'bar';
$ref = &$obj->foo;
$obj2 = clone $obj;
$obj->foo = 'baz'; // $obj->foo, $ref, and $obj2->foo now all have "baz"
Can someone please give a more in-depth explanation of what's going on here and maybe correct any inaccuracies in what I described? Thanks!
View all comments
Show parent comment
2
u/obstreperous_troll Apr 15 '25 edited Apr 15 '25
The exact semantics of cloning are defined by a class's
__clonemethod, but the default behavior is a "shallow copy": for all properties, the clone shares the same value as the original, only increases the refcount, and any ref-ness stays intact.References are seen by a lot of the core devs as a wart. Not one PHP can easily get rid of, but that should be a goal nonetheless. Any performance gains they provide can be had by other means (possibly by keeping references but restricting them to local scope)
Demo of the behavior I'm talking about here: https://3v4l.org/1PHp1 ... it's even weirder since I accidentally made it a circular reference instead of pointing to $message, but it was sharing of the "ref-ness" that was important. Sadly we can't write
become()in pure PHP ;)