echo $foo; //typecast to a string automatically
$myVar = $foo; //SimpleXMLElement
$myVar = (string)$foo; //typecast to a string
The SimpleXMLElement behaves like an object which is why people get confused like you did, but it's actually a language construct which is a little different. The elements and their data must be typecast to the data type you want to work with because by default everything returned from a SimpleXMLElement function is a SimpleXMLElement which if not typecast by a function, must be directly typecast.
If you call:
echo $foo; //typecast to a string automatically
$myVar = $foo; //SimpleXMLElement
$myVar = (string)$foo; //typecast to a string
发布评论
评论(2)
SimpleXMLElement 的行为就像一个对象,这就是为什么人们会像您一样感到困惑,但它实际上是一种有点不同的语言构造。元素及其数据必须类型转换为您想要使用的数据类型,因为默认情况下从 SimpleXMLElement 函数返回的所有内容都是 SimpleXMLElement,如果不是由函数类型转换,则必须直接类型转换。
如果您致电:
The SimpleXMLElement behaves like an object which is why people get confused like you did, but it's actually a language construct which is a little different. The elements and their data must be typecast to the data type you want to work with because by default everything returned from a SimpleXMLElement function is a SimpleXMLElement which if not typecast by a function, must be directly typecast.
If you call:
这有效:
This works: