$xml_str = <<
CNY
SUCCESS
NATIVE
EOT;$obj = simplexml_load_string($xml_str);var_dump($obj->game);if(empty($obj->game)) { echo '空的';} else { echo "不空";}
The game attribute does not exist, but Var_dump has the result, what is the reason?
Reply content:
$xml_str = <<
CNY
SUCCESS
NATIVE
EOT;$obj = simplexml_load_string($xml_str);var_dump($obj->game);if(empty($obj->game)) { echo '空的';} else { echo "不空";}
The game attribute does not exist, but Var_dump has the result, what is the reason?
Although the game
property does not exist, but the direct fetch of this property, SimpleXMLElement
the Magic method is called, __get()
this method returns an empty object, and is var_dump
printed out.
But the object empty()
should be true when judging, why is this false?
Because it empty()
is not equal to the direct Boolean judgment, but rather empty($var) === isset($var) && $var
, so here will first call SimpleXMLElement
the Magic method __isset()
, and __isset()
return this property does not exist, the following is __get()
automatically skipped.
Because simplexmlelement
internally implemented setter
and getter
, like magic methods __set()
and __get()
,