PHP:SimpleXMLElement() 类和 SimpleXML_load_string() 之间有什么区别? [英] PHP: Whats's the difference between the SimpleXMLElement() class and 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.
推荐答案
simplexml_load_string()
(顾名思义)从字符串中加载 xml 并返回 SimepleXMLElement
.这与仅使用类的常用构造函数没有区别.
simplexml_load_string()
(as the name suggest) load xml from a string and returns an object of SimepleXMLElement
. There is no difference between this and using just the usual constructor of the class.
更新:
SimpleXML::__construct()
有一个附加参数(第三个)bool $data_is_url = false
.如果这是 true
,则行为与 simplexml_load_file()
相同(与通用流包装器结合使用).因此,两个 simplexml_load_*()
函数都涵盖了与 SimpleXML::__construct()
相同的功能.
SimpleXML::__construct()
has an additional parameter (the third one) bool $data_is_url = false
. If this is true
the behavior is the same as simplexml_load_file()
(in conjunction with the common stream wrappers). Thus both simplexml_load_*()
-functions cover the same functionality as SimpleXML::__construct()
.
另外的函数 simplexml_load_*()
有第二个参数 string $class_name = "SimpleXMLElement"
来指定要返回的对象的类.这不是函数的特定功能,因为您也可以使用"与通常的实例化非常相似的东西
Additional the functions simplexml_load_*()
has a second parameter string $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 too
class MyXML extends SimpleXMLElement {}
$a = new MyXML($xml);
$b = simplexml_load_string($xml, 'MyXML');
OOP 方法和过程方法之间的区别在于,函数在出错时返回 false
,但构造函数会抛出异常.
A difference between the OOP- and the procedural approach is, that the functions return false
on error, but the constructor throws an Exception.
这篇关于PHP:SimpleXMLElement() 类和 SimpleXML_load_string() 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!