I was doing some benchmarking tests with PHP references and I came across a curious example.
$a->b->c->d->e->f->g->h->i->j =& $null;
Both variables $a
and Null
do not exist in any scope of my script.
When I give print_r($a)
, see what is returned:
stdClass Object
(
[b] => stdClass Object
(
[c] => stdClass Object
(
[d] => stdClass Object
(
[e] => stdClass Object
(
[f] => stdClass Object
(
[g] => stdClass Object
(
[h] => stdClass Object
(
[i] => stdClass Object
(
[j] =>
)
)
)
)
)
)
)
)
)
The strange thing is that when I try to do this without reference, look what happens:
$a->b->c = 1;
PHP Warning: Creating default object from empty value on line 1
Why is this happening in PHP? What is the reason for this behavior (the object being created out of nowhere and not having issued any Warning)?
Why did the $a
and $null
variables not return Undefined variable
when I used references?