PHP:SimpleXMLElement() 类和 SimpleXML_load_string() 之间有什么区别?

发布于 2024-11-08 22:26:24 字数 240 浏览 1 评论 0原文

我见过很多程序员实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云雾 2024-11-15 22:26:24

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" 来指定要返回的对象的类。这不是函数的特定功能,因为您也可以“使用”与通常实例化非常相似的东西

class MyXML extends SimpleXMLElement {}
$a = new MyXML($xml);
$b = simplexml_load_string($xml, 'MyXML');

OOP 和过程方法之间的区别是,函数在出错时返回 false,但是构造函数抛出异常。

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.

Update:

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().

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');

A difference between the OOP- and the procedural approach is, that the functions return false on error, but the constructor throws an Exception.

美煞众生 2024-11-15 22:26:24

它主要是一个方便的包装。通过自己构建基本元素,您至少需要两行代码才能完成任何事情。使用 simplexml_load_string() 单个表达式可能就足够了:

 print simplexml_load_string($xml)->title;

短于:

 $e = new SimpleXMLElement($xml);
 print $e->title;

然后当然,函数签名也有细微的变化。

更新:并且长度与

完全相同

print(new SimpleXMLElement($xml))->title;

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:

 print simplexml_load_string($xml)->title;

Is shorter than:

 $e = new SimpleXMLElement($xml);
 print $e->title;

And then of course, there is also the slight variation in the function signature.

Update: And exactly the same length as of

print(new SimpleXMLElement($xml))->title;
伪心 2024-11-15 22:26:24

simplexml_load_string 可以返回不同的对象:

类名

您可以使用此可选参数,以便
simplexml_load_string() 将返回一个
指定类的对象。那
类应该扩展
SimpleXMLElement 类。

simplexml_load_string can return a different object:

class_name

You may use this optional parameter so that
simplexml_load_string() will return an
object of the specified class. That
class should extend the
SimpleXMLElement class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文