Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty() returns false for an empty SimpleXMLElement #3963

Closed
aingram opened this issue Oct 13, 2014 · 6 comments
Closed

empty() returns false for an empty SimpleXMLElement #3963

aingram opened this issue Oct 13, 2014 · 6 comments

Comments

@aingram
Copy link
Contributor

aingram commented Oct 13, 2014

In Zend, calling empty() on a nested xml element of a SimpleXMLElement returns true when the element has no text nor children. In HHVM it returns false. For example,

<?php
$xml = new SimpleXMLElement('<foo><bar/></foo>');
echo empty($xml->bar) ? "empty\n" : "not empty\n";

However, in a simpler example, a single element by itself will correctly return true in both Zend and HHVM:

<?php
$xml = new SimpleXMLElement('<bar/>');
echo empty($xml) ? "empty\n" : "not empty\n";

Tested on libxml 2.7.8, HHVM 3.3, and PHP 5.3

// @danslo

@aingram
Copy link
Contributor Author

aingram commented Oct 26, 2014

When I set a breakpoint in Zend PHP in sxe_object_cast_ex, the breakpoint is not hit in the first example above but it is hit in the second example. In HHVM, the equivalent breakpoint in sxe_object_cast is hit in both examples.

@aingram
Copy link
Contributor Author

aingram commented Oct 26, 2014

Zend has a different call stack when calling empty with a variable, e.g. $foo, versus calling it with the property of an object, e.g. $foo->bar. In the former case, Zend simply returns !(bool)$foo. In the latter, Zend uses its object handler framework to determine which function to call based on what type $foo is. For a SimpleXmlElement, it calls sxe_property_exists. HHVM calls !(bool)... in both cases which is why there's inconsistent behavior.

@hamidre13
Copy link
Contributor

I will look at it

@paulbiss
Copy link
Contributor

paulbiss commented Nov 3, 2014

#4119

danslo added a commit to danslo/hhvm that referenced this issue Nov 3, 2014
@danslo
Copy link
Contributor

danslo commented Nov 6, 2014

The problem is basically this:

The PHP documentation states for empty(): That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

However, SimpleXML decides that's nonsense:

<?php
$xml = new SimpleXMLElement('<foo><bar/></foo>');
var_dump(isset ($xml->bar));  // true
var_dump((bool)($xml->bar));  // true
var_dump(empty($xml->bar));   // true? wat.

Anyone with php internals knowledge that knows what php-src does for figuring out empty() in this case? @sgolemon @auroraeosrose

@danslo
Copy link
Contributor

danslo commented Nov 12, 2014

@paulbiss This can be closed as it has been solved in #4204.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants