3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Test extends ArrayIterator { public function __toString() { return (string) $this[0]; } } 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->offsetGet($offset)); } public function offsetGet($offset) { if ($this->xml->getName() == $offset) { return $this; } return new Test((array)$this->xml->$offset); } 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']; echo $xml['note']['recursive']['val'][0]; var_dump($xml->getIterator());
Output for 5.1.3 - 7.1.0
Tove2object(SimpleXMLIterator)#2 (5) { ["to"]=> string(4) "Tove" ["from"]=> string(4) "Jani" ["heading"]=> string(8) "Reminder" ["body"]=> string(29) "Don't forget me this weekend!" ["recursive"]=> object(SimpleXMLIterator)#3 (1) { ["val"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } } }
Output for 5.1.2
Fatal error: Call to undefined method SimpleXMLIterator::getName() in /in/5vDh6 on line 28
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/5vDh6:18 Stack trace: #0 /in/5vDh6(18): SimpleXMLElement->__construct('<?xml version="...', 0, false) #1 /in/5vDh6(68): XmlArrayAccessIterator->__construct('<?xml version="...', false) #2 {main} thrown in /in/5vDh6 on line 18
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/5vDh6:18 Stack trace: #0 /in/5vDh6(18): SimpleXMLIterator->__construct('<?xml version="...', 0, false) #1 /in/5vDh6(68): XmlArrayAccessIterator->__construct('<?xml version="...', false) #2 {main} thrown in /in/5vDh6 on line 18
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/5vDh6:18 Stack trace: #0 /in/5vDh6(68): XmlArrayAccessIterator->__construct('<?xml version="...', 0, false) #1 {main} thrown in /in/5vDh6 on line 18
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/5vDh6 on line 5
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 T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/5vDh6 on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/5vDh6 on line 5
Process exited with code 255.