将字符串转换为xml对象[php]的更好表达式是什么?

时间:2022-10-27 00:25:03

which is better expression to make string into xml object[php]?

将字符串转换为xml对象[php]的更好表达式是什么?

$xml = new SimpleXMLElement($xml_string);

vs

vs

$xml = simplexml_load_string($xml_string);

same?

相同吗?

3 个解决方案

#1


2  

They're basically identical. SimpleXMLElement::__construct accepts data strings or file paths, if you set the appropriate options. simplexml_load_file and simplexml_load_string are basically convenience function wrappers that amount to the same thing as new SimpleXMLElement() with the correct options set.

他们基本上相同。__construct接受数据字符串或文件路径,如果你设置了合适的选项。simplexml_load_file和simplexml_load_string基本上是一种方便的函数包装器,它们与新的SimpleXMLElement()具有正确的选项集。

#2


2  

They're essentially the same. Which one you use is based on personal taste. I'd go for the first. The simplexml_load_string function will create an object too, which makes it essentially an alias for the constructor.

它们本质上是相同的。你使用哪一个是基于个人喜好。我会第一个去。simplexml_load_string函数也将创建一个对象,这实际上使它成为构造函数的别名。

#3


2  

The simplexml_load_string lets you specify the class of which you want to get the object of. If you call the simplexml_load_string without specifying the class as second parameter, then it automatically gives you the object of SimpleXMLElement.

simplexml_load_string允许指定要获取对象的类。如果您调用simplexml_load_string而没有将类指定为第二个参数,那么它将自动为您提供SimpleXMLElement的对象。

So, in the way you are running it, they both will give you same results.

所以,在你运行它的过程中,它们都会给你相同的结果。

#1


2  

They're basically identical. SimpleXMLElement::__construct accepts data strings or file paths, if you set the appropriate options. simplexml_load_file and simplexml_load_string are basically convenience function wrappers that amount to the same thing as new SimpleXMLElement() with the correct options set.

他们基本上相同。__construct接受数据字符串或文件路径,如果你设置了合适的选项。simplexml_load_file和simplexml_load_string基本上是一种方便的函数包装器,它们与新的SimpleXMLElement()具有正确的选项集。

#2


2  

They're essentially the same. Which one you use is based on personal taste. I'd go for the first. The simplexml_load_string function will create an object too, which makes it essentially an alias for the constructor.

它们本质上是相同的。你使用哪一个是基于个人喜好。我会第一个去。simplexml_load_string函数也将创建一个对象,这实际上使它成为构造函数的别名。

#3


2  

The simplexml_load_string lets you specify the class of which you want to get the object of. If you call the simplexml_load_string without specifying the class as second parameter, then it automatically gives you the object of SimpleXMLElement.

simplexml_load_string允许指定要获取对象的类。如果您调用simplexml_load_string而没有将类指定为第二个参数,那么它将自动为您提供SimpleXMLElement的对象。

So, in the way you are running it, they both will give you same results.

所以,在你运行它的过程中,它们都会给你相同的结果。