Type of non existent SimpleXMLElements

32 Views Asked by At

Can you explain me the reason why SimpleXMLElement returns an empty object for non-existent XML-elements on the first level, but not for any layer below?

See this code:

$xml = simplexml_load_string('<root><Foo></Foo></root>');

echo "Type of A: " . gettype($xml->Foo->A) . PHP_EOL; // object
echo "Type of B: " . gettype($xml->Foo->A->B) . PHP_EOL; // NULL
echo "Type of C: " . gettype($xml->Foo->A->B->C) . PHP_EOL; // Warning because of property access on NULL

var_dump($xml->Foo->A); //object(SimpleXMLElement)#4 (0) {}
0

There are 0 best solutions below