<?php
class Test extends ArrayIterator
{
public function __toString()
{
/*if (count($this) > 1) {
return (string) $this[0];
}*/
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->xml->$offset);
}
public function offsetGet($offset)
{
if ($this->xml->getName() == $offset) {
return $this;
}
return new Test((array)$this->xml->$offset);
}
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'];
echo $xml['note']['recursive']['val'][1];
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/KsESC on line 24
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/KsESC on line 29
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/KsESC 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/KsESC 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/KsESC on line 53
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/KsESC on line 20
Tove4
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/KsESC on line 24
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/KsESC on line 29
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/KsESC 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/KsESC 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/KsESC on line 53
Tove4
Fatal error: Call to undefined method SimpleXMLIterator::getName() in /in/KsESC on line 31
Process exited with code 255.
Output for 5.0.5, 5.1.0 - 5.1.1
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/KsESC:21
Stack trace:
#0 /in/KsESC(21): SimpleXMLElement->__construct('<?xml version="...', 0, false)
#1 /in/KsESC(76): XmlArrayAccessIterator->__construct('<?xml version="...', false)
#2 {main}
thrown in /in/KsESC on line 21
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/KsESC:21
Stack trace:
#0 /in/KsESC(21): SimpleXMLIterator->__construct('<?xml version="...', 0, false)
#1 /in/KsESC(76): XmlArrayAccessIterator->__construct('<?xml version="...', false)
#2 {main}
thrown in /in/KsESC on line 21
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/KsESC:21
Stack trace:
#0 /in/KsESC(76): XmlArrayAccessIterator->__construct('<?xml version="...', 0, false)
#1 {main}
thrown in /in/KsESC on line 21
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/KsESC on line 5
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/KsESC 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/KsESC on line 5
Process exited with code 255.