3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ObjectAndXML { private static $xml; // Constructor public function __construct() { $this->xml = new XmlWriter(); $this->xml->openMemory(); $this->xml->startDocument('1.0'); $this->xml->setIndent(true); } // Method to convert Object into XML string public function objToXML($obj) { $this->getObject2XML($this->xml, $obj); $this->xml->endElement(); return $this->xml->outputMemory(true); } // Method to convert XML string into Object public function xmlToObj($xmlString) { return simplexml_load_string($xmlString); } private function getObject2XML(XMLWriter $xml, $data) { foreach($data as $key => $value) { if(is_object($value)) { $xml->startElement($key); $this->getObject2XML($xml, $value); $xml->endElement(); continue; } else if(is_array($value)) { $this->getArray2XML($xml, $key, $value); } if (is_string($value)) { $xml->writeElement($key, $value); } } } private function getArray2XML(XMLWriter $xml, $keyParent, $data) { foreach($data as $key => $value) { if (is_string($value)) { $xml->writeElement($keyParent, $value); continue; } if (is_numeric($key)) { $xml->startElement($keyParent); } if(is_object($value)) { $this->getObject2XML($xml, $value); } else if(is_array($value)) { $this->getArray2XML($xml, $key, $value); continue; } if (is_numeric($key)) { $xml->endElement(); } } } } $obj = new ObjectAndXML(); $str = <<<STR <?xml version="1.0" encoding="utf-8"?> <records> <person> <name>XYZ</name> <age>28</age> <gender>Male</gender> </person> <person> <name>ABC</name> <age>25</age> <gender>Male</gender> </person> </records> STR; $recordsObj = $obj->xmlToObj($str); echo '<pre>'; var_dump($recordsObj);
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 8 Deprecated: Creation of dynamic property ObjectAndXML::$xml is deprecated in /in/sbU2K on line 8 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 9 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 10 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 11 <pre>object(SimpleXMLElement)#3 (1) { ["person"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (3) { ["name"]=> string(3) "XYZ" ["age"]=> string(2) "28" ["gender"]=> string(4) "Male" } [1]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(3) "ABC" ["age"]=> string(2) "25" ["gender"]=> string(4) "Male" } } }
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 8 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 9 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 10 Notice: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 11 <pre>object(SimpleXMLElement)#3 (1) { ["person"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (3) { ["name"]=> string(3) "XYZ" ["age"]=> string(2) "28" ["gender"]=> string(4) "Male" } [1]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(3) "ABC" ["age"]=> string(2) "25" ["gender"]=> string(4) "Male" } } }
Output for 7.3.32 - 7.3.33
<pre>object(SimpleXMLElement)#3 (1) { ["person"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (3) { ["name"]=> string(3) "XYZ" ["age"]=> string(2) "28" ["gender"]=> string(4) "Male" } [1]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(3) "ABC" ["age"]=> string(2) "25" ["gender"]=> string(4) "Male" } } }
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 8 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 9 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 10 Strict Standards: Accessing static property ObjectAndXML::$xml as non static in /in/sbU2K on line 11 <pre>object(SimpleXMLElement)#3 (1) { ["person"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (3) { ["name"]=> string(3) "XYZ" ["age"]=> string(2) "28" ["gender"]=> string(4) "Male" } [1]=> object(SimpleXMLElement)#5 (3) { ["name"]=> string(3) "ABC" ["age"]=> string(2) "25" ["gender"]=> string(4) "Male" } } }

preferences:
171.71 ms | 403 KiB | 179 Q