empty() returns false for an empty SimpleXMLElement
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
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.
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.
I will look at it
#4119
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
@paulbiss This can be closed as it has been solved in #4204.