2

I am trying to perform count on SimpleXML Element. It si giving me different results on PHP 5.3 and PHP 5.2. My code looks like follows :

$xml = new SimpleXMLElement('<command action="foo"/>');
print_r(count((array)$xml->children()));`

On PHP 5.2 the above prints "1" and on PHP 5.3 it prints "0" :( I know I can use $xml->count but that does not take ino account the root element of the XML.

Just wondering what might be wrong in type casting the SimpleXML to array in PHP 5.3

deceze
  • 510,633
  • 85
  • 743
  • 889
PHP_dude
  • 23
  • 2

1 Answers1

0

Well, SimpleXMLElement::children() will always return a SimpleXMLElement instance according to the manual.

And it does according to var_dump.

But as command has no child, the returned object has no accessible attributes which yields to 0 when casting to array:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Couldn't find anything about changed type casting to array for 5.2 to 5.3.

So this might (might) be a bug ...

Raffael
  • 19,547
  • 15
  • 82
  • 160