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 (preg_match('/(\w+)\[(\d+)\]/', $offset, $nodes)) { var_dump($this->xml->$nodes[1][0]->asXML()); return new self($this->xml->$nodes[1][$nodes[2]]->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']; $xml['note']['recursive']['val[1]'];
Output for 7.0.0 - 7.1.0
Notice: Array to string conversion in /in/YCZoH on line 25 Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/YCZoH:25 Stack trace: #0 /in/YCZoH(72): XmlArrayAccessIterator->offsetGet('val[1]') #1 {main} thrown in /in/YCZoH on line 25
Process exited with code 255.
Output for hhvm-3.12.0
bool(false) Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(10): SimpleXMLElement->__construct() #1 /in/YCZoH(26): XmlArrayAccessIterator->__construct() #2 /in/YCZoH(72): XmlArrayAccessIterator->offsetGet() #3 {main}
Process exited with code 255.
Output for hhvm-3.10.0
bool(false) Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(10): SimpleXMLIterator->__construct() #1 /in/YCZoH(26): XmlArrayAccessIterator->__construct() #2 /in/YCZoH(72): XmlArrayAccessIterator->offsetGet() #3 {main}
Process exited with code 255.
Output for 5.2.3 - 5.6.28
bool(false) Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(10): SimpleXMLElement->__construct('', 0, false) #1 /in/YCZoH(26): XmlArrayAccessIterator->__construct(false, false) #2 /in/YCZoH(72): XmlArrayAccessIterator->offsetGet('val[1]') #3 {main} thrown in /in/YCZoH on line 10
Process exited with code 255.
Output for 5.1.3 - 5.2.2
bool(false) <br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(10): SimpleXMLElement-&gt;__construct('', 0, false) #1 /in/YCZoH(26): XmlArrayAccessIterator-&gt;__construct(false, false) #2 /in/YCZoH(72): XmlArrayAccessIterator-&gt;offsetGet('val[1]') #3 {main} thrown in <b>/in/YCZoH</b> on line <b>10</b><br />
Process exited with code 255.
Output for 5.1.2
<br /> <b>Fatal error</b>: Call to undefined method SimpleXMLIterator::getName() in <b>/in/YCZoH</b> on line <b>20</b><br />
Process exited with code 255.
Output for 5.0.5 - 5.1.1
<br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(10): SimpleXMLElement->__construct('<?xml version="...', 0, false) #1 /in/YCZoH(68): XmlArrayAccessIterator->__construct('<?xml version="...', false) #2 {main} thrown in <b>/in/YCZoH</b> on line <b>10</b><br />
Process exited with code 255.
Output for 5.0.3 - 5.0.4
<br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(10): SimpleXMLIterator->__construct('<?xml version="...', 0, false) #1 /in/YCZoH(68): XmlArrayAccessIterator->__construct('<?xml version="...', false) #2 {main} thrown in <b>/in/YCZoH</b> on line <b>10</b><br />
Process exited with code 255.
Output for 5.0.0 - 5.0.2
<br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/YCZoH:10 Stack trace: #0 /in/YCZoH(68): XmlArrayAccessIterator->__construct('<?xml version="...', 0, false) #1 {main} thrown in <b>/in/YCZoH</b> on line <b>10</b><br />
Process exited with code 255.
Output for 4.4.2 - 4.4.9
<br /> <b>Parse error</b>: syntax error, unexpected T_STRING, expecting '{' in <b>/in/YCZoH</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
<br /> <b>Parse error</b>: parse error, unexpected T_STRING, expecting '{' in <b>/in/YCZoH</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error, expecting `'{'' in <b>/in/YCZoH</b> on line <b>3</b><br />
Process exited with code 255.