Home > Backend Development > PHP Tutorial > Use the object returned by simplexml_load_string($xml_str) to access non-existent attributes, empty is true

Use the object returned by simplexml_load_string($xml_str) to access non-existent attributes, empty is true

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-29 08:50:55
Original
1328 people have browsed it

<code>$xml_str = <<<EOT
    <xml>
        <fee_type><![CDATA[CNY]]></fee_type>
        <return_code><![CDATA[SUCCESS]]></return_code>
        <trade_type><![CDATA[NATIVE]]></trade_type>
    </xml>
EOT;
$obj = simplexml_load_string($xml_str);
var_dump($obj->game);
if(empty($obj->game)) {
    echo '空的';
} else {
    echo "不空";
}
</code>
Copy after login
Copy after login

The game attribute does not exist, but var_dump has results. Why?
Use the object returned by simplexml_load_string($xml_str) to access non-existent attributes, empty is true

Reply content:

<code>$xml_str = <<<EOT
    <xml>
        <fee_type><![CDATA[CNY]]></fee_type>
        <return_code><![CDATA[SUCCESS]]></return_code>
        <trade_type><![CDATA[NATIVE]]></trade_type>
    </xml>
EOT;
$obj = simplexml_load_string($xml_str);
var_dump($obj->game);
if(empty($obj->game)) {
    echo '空的';
} else {
    echo "不空";
}
</code>
Copy after login
Copy after login

The game attribute does not exist, but var_dump has results. Why?
Use the object returned by simplexml_load_string($xml_str) to access non-existent attributes, empty is true

Although the game attribute does not exist, when directly obtaining this attribute, the magic method __get() of SimpleXMLElement will be called. This method returns an empty object and is printed out by var_dump.
But the object should be true when empty() is judged. Why is it false here?
Because empty() is not equal to a direct Boolean judgment, but empty($var) === isset($var) && $var, so the magic method __isset( of SimpleXMLElement will be called first) ), and __isset() returns that this attribute does not exist, and the following __get() is automatically skipped.

Because simplexmlelement internally implements setter and getter, just like the magic methods __set() and __get(),

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template