PHP already does a copy-on-write thing where it doesn't create a new value til you change something, so there's not much memory saved by using references. Doing so can even mess with some stuff PHP does internally to reduce memory usage, making things even worse.
Add to that the fact that references make things a bit too magical in general. The default, and thus what most people expect, is pass-by-value; when i pass $i
to a function, it complicates things tremendously to have to care whether that function mysteriously changes $i
to something else entirely, and thus make defensive copies just in case. (It can already modify $i
if the value is an object, but in my opinion it shouldn't.)
Basically, i'd only find pass-by-reference useful for "out" parameters, meaning variables i expect to get back from the function rather than pass in, a la preg_match
's &$matches
. Even for functions that clearly modify the object being passed in, like sort
or array_pop
, that feels a bit icky...but that's what we're stuck with.
return array($foo, $bar);
andlist($A, $B) = foobar();