PHP:SimpleXMLElement() 类和 SimpleXML_load_string() 之间有什么区别?
我见过很多程序员实现 SimpleXML_load_string()
而不是 SimpleXMLElement()
类。使用前者比后者有什么好处吗?我已阅读有关 simplexml 的 PHP 手册。我无法理解它是什么。
任何帮助和指导(也许通过示例)将不胜感激。提前致谢。
I've seen alot of coders implement SimpleXML_load_string()
instead of the SimpleXMLElement()
class. Are there any benefits to using the former over the latter? I've read the PHP manual on simplexml. I couldn't manage to get a grasp on what it is.
Any help and guidance (perhaps through examples) would be much appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
simplexml_load_string()
(顾名思义)从字符串加载 xml 并返回SimepleXMLElement
对象。这与仅使用类的常用构造函数没有区别。更新:
SimpleXML::__construct()
有一个附加参数(第三个)bool $data_is_url = false
。如果这是true
,则行为与simplexml_load_file()
相同(与公共流包装器结合使用)。因此,两个simplexml_load_*()
函数涵盖与SimpleXML::__construct()
相同的功能。另外,函数
simplexml_load_*()
有第二个参数string $class_name = "SimpleXMLElement"
来指定要返回的对象的类。这不是函数的特定功能,因为您也可以“使用”与通常实例化非常相似的东西OOP 和过程方法之间的区别是,函数在出错时返回
false
,但是构造函数抛出异常。simplexml_load_string()
(as the name suggest) load xml from a string and returns an object ofSimepleXMLElement
. There is no difference between this and using just the usual constructor of the class.Update:
SimpleXML::__construct()
has an additional parameter (the third one)bool $data_is_url = false
. If this istrue
the behavior is the same assimplexml_load_file()
(in conjunction with the common stream wrappers). Thus bothsimplexml_load_*()
-functions cover the same functionality asSimpleXML::__construct()
.Additional the functions
simplexml_load_*()
has a second parameterstring $class_name = "SimpleXMLElement"
to specify the class of the object to get returned. Thats not a specific feature of the functions, because you can "use" something very similar with usual instanciation tooA difference between the OOP- and the procedural approach is, that the functions return
false
on error, but the constructor throws an Exception.它主要是一个方便的包装。通过自己构建基本元素,您至少需要两行代码才能完成任何事情。使用
simplexml_load_string()
单个表达式可能就足够了:短于:
然后当然,函数签名也有细微的变化。
It's mostly a convenience wrapper. With constructing the base element yourself, you need at least two lines of code to accomplish anything. With
simplexml_load_string()
a single expression might suffice:Is shorter than:
And then of course, there is also the slight variation in the function signature.
simplexml_load_string 可以返回不同的对象:
simplexml_load_string can return a different object: