In PHP, according to the manual, the reference of one variable is for one variable pointing to the same place where the other is in memory. I also saw that when we declare we refer to a variable that does not exist, it is created and set to NULL
.
Then came a question.
We have the following codes.
In this allocation attempt below, a runtime error will be generated.
$a = $b->b->c->d->e->f->g;
/*
Undefined variable 'a'
Trying to get property of non-object (6 vezes)
*/
var_dump($a, $b); // resultado: NULL NULL
Now, when we use the assignment operator by reference ( &
), no error is generated, and the object (which does not exist) is magically created.
$a =& $b->b->c->d->e->f->g;
var_dump($a);
/*
a = NULL
*/
print_r($b);
/*
stdClass Object
(
[b] => stdClass Object
(
[c] => stdClass Object
(
[d] => stdClass Object
(
[e] => stdClass Object
(
[f] => stdClass Object
(
[g] =>
)
)
)
)
)
)
*/
PHP
.