Could anyone explain why the following works in PHP?
$xml = \simplexml_load_string('<root></root>');
$xml->a->b->c = 123;
(And any depth will work.)
But when trying to inspect the item directly, it will, as I would expect, throw a reference error.
$xml = \simplexml_load_string('<root></root>');
var_dump($xml->a->b->c);
Because, $xml->a
is a SimpleXMLElement, and $x->a-b
is null, and then you can't reference properties of non-objects.
Even in the first situation of the assignment, I would expect the dereferencing to happen before the assignment and fail. But it looks like there's something working differently there.