$a = 'b';
$b = 'a';
echo $a."\n";
>>> b
echo $$a."\n";
>>> a
echo $$$a."\n";
>>> b
Yaaa $obj = new StdClass();
$obj->x = 1;
function x_plus_one($obj) {
$obj->x++;
}
x_plus_one($obj);
var_dump($obj);
>>> object(stdClass)#1 (1) {
>>> ["x"]=>
>>> int(2)
>>> }
Huh? A local change to the object changed the object outside the local scope? It must have been passed by reference... function null_the_object($obj) {
$obj = null;
}
null_the_object($obj);
var_dump($obj);
>>> object(stdClass)#1 (1) {
>>> ["x"]=>
>>> int(2)
>>> }
Huh? Before when I edited the object within the function, the object I passed in was edited, what gives?We don't actually pass objects to reference in PHP, we pass them by "object reference." But don't worry, you can still null objects that get passed to you:
function null_the_object_2(&$obj) {
$obj = null;
}
null_the_object_2($obj);
var_dump($obj);
>>> NULL
Sure it's not as bad as it used to be. Yes, a disciplined developer can make good software with it, but it's still a pretty quirky.