3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class XmlArrayAccessIterator implements ArrayAccess, IteratorAggregate { private $xml; public function __construct($filePath, $isUrl = true) { $this->isUrl = $isUrl; $this->xml = new SimpleXmlIterator($filePath, 0, $isUrl); } public function offsetExists($offset) { return !is_null($this->xml->$offset); } public function offsetGet($offset) { if ($this->xml->getName() == $offset) { return $this; } if (is_int($offset)) { $tag = $this->xml->getName(); $target = $this->xml->xpath('../' . $tag . '[' . $offset . ']'); //$target = $target[0]; return new self($target->asXML(), false); } return new self($this->xml->$offset->asXML(), false); } public function __toString() { return (string) $this->xml; } public function offsetSet($offset, $value) { throw new RuntimeException('This is readonly. writing to "' . (string) $offset . '" with value "' . (string) $value . '" not possible'); } public function offsetUnset($offset) { throw new RuntimeException('This is readonly. Unsetting "' . (string) $offset . '" not possible'); } public function getIterator() { return $this->xml; } } $str = '<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don\'t forget me this weekend!</body> <recursive> <val>2</val> <val>4</val> </recursive> </note>'; $xml = new XmlArrayAccessIterator($str, false); //echo $xml['note']['to']; var_dump($xml['note']['recursive']['val'][1]);
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to a member function asXML() on array in /in/7aPWR:28 Stack trace: #0 /in/7aPWR(75): XmlArrayAccessIterator->offsetGet(1) #1 {main} thrown in /in/7aPWR on line 28
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function asXML() on a non-object (array)' in /in/7aPWR:28 Stack trace: #0 /in/7aPWR(75): XmlArrayAccessIterator->offsetGet() #1 {main}
Process exited with code 255.
Output for 5.6.0 - 5.6.28
Fatal error: Call to a member function asXML() on array in /in/7aPWR on line 28
Process exited with code 255.
Output for 5.1.3 - 5.5.38
Fatal error: Call to a member function asXML() on a non-object in /in/7aPWR on line 28
Process exited with code 255.
Output for 5.1.2
Fatal error: Call to undefined method SimpleXMLIterator::getName() in /in/7aPWR on line 20
Process exited with code 255.
Output for 5.0.5 - 5.1.1
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/7aPWR:10 Stack trace: #0 /in/7aPWR(10): SimpleXMLElement->__construct('<?xml version="...', 0, false) #1 /in/7aPWR(72): XmlArrayAccessIterator->__construct('<?xml version="...', false) #2 {main} thrown in /in/7aPWR on line 10
Process exited with code 255.
Output for 5.0.3 - 5.0.4
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/7aPWR:10 Stack trace: #0 /in/7aPWR(10): SimpleXMLIterator->__construct('<?xml version="...', 0, false) #1 /in/7aPWR(72): XmlArrayAccessIterator->__construct('<?xml version="...', false) #2 {main} thrown in /in/7aPWR on line 10
Process exited with code 255.
Output for 5.0.0 - 5.0.2
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/7aPWR:10 Stack trace: #0 /in/7aPWR(72): XmlArrayAccessIterator->__construct('<?xml version="...', 0, false) #1 {main} thrown in /in/7aPWR on line 10
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/7aPWR on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/7aPWR on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/7aPWR on line 3
Process exited with code 255.