简体   繁体   English

PHP 5.3 SimpleXML进行数组类型转换的问题

[英]PHP 5.3 SimpleXML to array type casting issue

I am trying to perform count on SimpleXML Element. 我正在尝试对SimpleXML Element执行计数。 It si giving me different results on PHP 5.3 and PHP 5.2. 这使我在PHP 5.3和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. 在PHP 5.2上,以上显示“ 1”,而在PHP 5.3上,显示“ 0” :(我知道我可以使用$xml->count但这并没有考虑XML的根元素。

Just wondering what might be wrong in type casting the SimpleXML to array in PHP 5.3 只是想知道在将简单XML类型转换为PHP 5.3中的数组时可能出什么问题

Well, SimpleXMLElement::children() will always return a SimpleXMLElement instance according to the manual. 好吧,根据手册, SimpleXMLElement :: children()将始终返回SimpleXMLElement实例。

And it does according to var_dump. 并根据var_dump执行。

But as command has no child, the returned object has no accessible attributes which yields to 0 when casting to array : 但是由于命令没有子对象,因此返回的对象没有可访问的属性, 当转换为array时 ,该属性的结果为0:

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 . 对于5.2到5.3的类型转换为数组找不到任何信息。

So this might (might) be a bug ... 因此,这可能(可能)是一个错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM