3v4l.org

run code in 300+ PHP versions simultaneously
<?php class XmlArrayAccessIterator implements ArrayAccess, IteratorAggregate { private $xml; private $current = 1; public function __construct($filePath, $isUrl = true) { $this->isUrl = $isUrl; $this->xml = new SimpleXmlIterator($filePath, 0, $isUrl); $this->current = $this->xml; } 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(); $parent = $this->xml->xpath('../'); $parent = $parent[0]; $target = $parent->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->current; } 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('test.xml'); //echo $xml['note']['to']; var_dump($xml['note']['recursive']);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of XmlArrayAccessIterator::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 15 Deprecated: Return type of XmlArrayAccessIterator::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 20 Deprecated: Return type of XmlArrayAccessIterator::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 43 Deprecated: Return type of XmlArrayAccessIterator::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 48 Deprecated: Return type of XmlArrayAccessIterator::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 53 Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/Cr908 on line 10 Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "test.xml" in /in/Cr908 on line 11 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement->__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml') #2 {main} thrown in /in/Cr908 on line 11
Process exited with code 255.
Output for 8.1.0 - 8.1.28
Deprecated: Return type of XmlArrayAccessIterator::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 15 Deprecated: Return type of XmlArrayAccessIterator::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 20 Deprecated: Return type of XmlArrayAccessIterator::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 43 Deprecated: Return type of XmlArrayAccessIterator::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 48 Deprecated: Return type of XmlArrayAccessIterator::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Cr908 on line 53 Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "test.xml" in /in/Cr908 on line 11 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement->__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml') #2 {main} thrown in /in/Cr908 on line 11
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "test.xml" in /in/Cr908 on line 11 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement->__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml') #2 {main} thrown in /in/Cr908 on line 11
Process exited with code 255.
Output for 5.2.3 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.35, 5.6.0 - 5.6.28
Warning: SimpleXMLElement::__construct(): I/O warning : failed to load external entity "test.xml" in /in/Cr908 on line 11 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement->__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml') #2 {main} thrown in /in/Cr908 on line 11
Process exited with code 255.
Output for 5.1.3 - 5.1.6, 5.2.0 - 5.2.2
<br /> <b>Warning</b>: SimpleXMLElement::__construct() [<a href='function.SimpleXMLElement---construct'>function.SimpleXMLElement---construct</a>]: I/O warning : failed to load external entity &quot;test.xml&quot; in <b>/in/Cr908</b> on line <b>11</b><br /> <br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement-&gt;__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator-&gt;__construct('test.xml') #2 {main} thrown in <b>/in/Cr908</b> on line <b>11</b><br />
Process exited with code 255.
Output for 5.1.2
<br /> <b>Warning</b>: SimpleXMLElement::__construct() [<a href='function.--construct'>function.--construct</a>]: I/O warning : failed to load external entity &quot;test.xml&quot; in <b>/in/Cr908</b> on line <b>11</b><br /> <br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement-&gt;__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator-&gt;__construct('test.xml') #2 {main} thrown in <b>/in/Cr908</b> on line <b>11</b><br />
Process exited with code 255.
Output for 5.0.5, 5.1.0 - 5.1.1
<br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLElement->__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml') #2 {main} thrown in <b>/in/Cr908</b> on line <b>11</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/Cr908:11 Stack trace: #0 /in/Cr908(11): SimpleXMLIterator->__construct('test.xml', 0, true) #1 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml') #2 {main} thrown in <b>/in/Cr908</b> on line <b>11</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/Cr908:11 Stack trace: #0 /in/Cr908(76): XmlArrayAccessIterator->__construct('test.xml', 0, true) #1 {main} thrown in <b>/in/Cr908</b> on line <b>11</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/Cr908</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
<br /> <b>Parse error</b>: parse error, unexpected T_STRING, expecting '{' in <b>/in/Cr908</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/Cr908</b> on line <b>3</b><br />
Process exited with code 255.

preferences:
258.61 ms | 401 KiB | 391 Q