hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

empty() returns false for an empty SimpleXMLElement

Open aingram opened this issue 12 years ago • 6 comments

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 avatar Oct 13 '14 02:10 aingram

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 avatar Oct 26 '14 07:10 aingram

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.

aingram avatar Oct 26 '14 10:10 aingram

I will look at it

hamidre13 avatar Oct 31 '14 22:10 hamidre13

#4119

paulbiss avatar Nov 03 '14 19:11 paulbiss

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 avatar Nov 06 '14 13:11 danslo

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

danslo avatar Nov 12 '14 02:11 danslo