3v4l.org

run code in 300+ PHP versions simultaneously
<?php class XmlArrayAccessIterator implements ArrayAccess, IteratorAggregate { const ATTRIBUTE_PREFIX = '@'; private $xml; public function __construct(SimpleXmlIterator $xml) { $this->xml = $xml; } private function getElement($offset) { $isAttribute = false; if (substr($offset, 0, 1) == self::ATTRIBUTE_PREFIX) { $offset = substr($offset, 1); $isAttribute = false; } var_dump($this->xml->$offset->asXML()); if (false !== $this->xml->$offset->asXML() && !$isAttribute) { $xml = $this->xml->$offset; } else { $xml = $this->xml[$offset]; } } public function offsetExists($offset) { return !is_null($this->getElement($offset)); } public function offsetGet($offset) { if ($this->xml->getName() == $offset) { return $this; } $xml = $this->getElement($offset); if (is_null($xml)) { throw new RuntimeException('unknown node or attribute "' . $offset . '"'); } return new self($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; } public function __toString() { return (string) $this->xml; } } $xml = '<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading foo="bar">Reminder</heading> <body>Don\'t forget me this weekend!</body> <recursive> <val>2</val> <val>4</val> </recursive> </note>'; $simple = new SimpleXmlIterator($xml); $xml = new XmlArrayAccessIterator($simple); echo $xml['note']['to']; echo $xml['note']['recursive']['val'][1]; echo $xml['note']['heading']['@foo'];
Output for 8.1.0 - 8.1.28, 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/WYgKV on line 31 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/WYgKV on line 36 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/WYgKV on line 51 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/WYgKV on line 56 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/WYgKV on line 61 string(13) "<to>Tove</to>" Fatal error: Uncaught RuntimeException: unknown node or attribute "to" in /in/WYgKV:45 Stack trace: #0 /in/WYgKV(91): XmlArrayAccessIterator->offsetGet('to') #1 {main} thrown in /in/WYgKV on line 45
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
string(13) "<to>Tove</to>" Fatal error: Uncaught RuntimeException: unknown node or attribute "to" in /in/WYgKV:45 Stack trace: #0 /in/WYgKV(91): XmlArrayAccessIterator->offsetGet('to') #1 {main} thrown in /in/WYgKV on line 45
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
string(13) "<to>Tove</to>" Fatal error: Uncaught exception 'RuntimeException' with message 'unknown node or attribute "to"' in /in/WYgKV:45 Stack trace: #0 /in/WYgKV(91): XmlArrayAccessIterator->offsetGet('to') #1 {main} thrown in /in/WYgKV on line 45
Process exited with code 255.
Output for 5.1.3 - 5.1.6, 5.2.0 - 5.2.2
string(13) "<to>Tove</to>" <br /> <b>Fatal error</b>: Uncaught exception 'RuntimeException' with message 'unknown node or attribute &quot;to&quot;' in /in/WYgKV:45 Stack trace: #0 /in/WYgKV(91): XmlArrayAccessIterator-&gt;offsetGet('to') #1 {main} thrown in <b>/in/WYgKV</b> on line <b>45</b><br />
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.2
<br /> <b>Fatal error</b>: Call to undefined method SimpleXMLIterator::getName() in <b>/in/WYgKV</b> on line <b>38</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/WYgKV</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/WYgKV</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/WYgKV</b> on line <b>3</b><br />
Process exited with code 255.

preferences:
311.13 ms | 401 KiB | 375 Q