<?php
class A implements ArrayAccess, IteratorAggregate {
private $_value = null;
public function __construct($value) {
$this->setValue($value);
}
/** {@inheritDoc} */
public function offsetGet($offset) {
if (!isset($this->_value[$offset])) {
trigger_error('The offset "' . $offset . '" is not defined for this variable', E_USER_NOTICE);
return null;
}
if (!$this->_value[$offset] instanceof self) {
$this->_value[$offset] = new self($this->_value[$offset]);
}
return $this->_value[$offset];
}
/** {@inheritDoc} */
public function offsetSet($offset, $value) {
if (!$value instanceof self) {
$value = new self($value);
}
$this->_value[$offset] = $value;
}
/** {@inheritDoc} */
public function offsetExists($offset) {
return isset($this->_value[$offset]);
}
/** {@inheritDoc} */
public function offsetUnset($offset) {
unset($this->_value[$offset]);
}
public function getValue() {
return $this->_value;
}
public function setValue($value) {
$this->_value = $value;
return $this;
}
public function getIterator() {
if ($this->_value instanceof Traversable) {
return new IteratorIterator($this->_value);
}
if (is_array($this->_value)) {
return new ArrayIterator($this->_value);
}
trigger_error('Variable not iterable', E_USER_WARNING);
return new EmptyIterator;
}
public function __toString() {
return (string) $this->getValue();
}
}
$a = new A([]);
$a['caca'] = 'prout';
$a['pipi'] = 'chouette';
foreach ($a as $k => $v) {
var_dump([$k, (string) $v]);
}
$a = new A('test');
foreach ($a as $k => $v) {
var_dump([$k, (string) $v]);
}
$a = new A(new ArrayObject(['variable', 'totally' => 'iterable']));
foreach ($a as $k => $v) {
var_dump([$k, (string) $v]);
}
Deprecated: Return type of A::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/AGvhG on line 33
Deprecated: Return type of A::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/AGvhG on line 10
Deprecated: Return type of A::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/AGvhG on line 24
Deprecated: Return type of A::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/AGvhG on line 38
Deprecated: Return type of A::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/AGvhG on line 52
array(2) {
[0]=>
string(4) "caca"
[1]=>
string(5) "prout"
}
array(2) {
[0]=>
string(4) "pipi"
[1]=>
string(8) "chouette"
}
Warning: Variable not iterable in /in/AGvhG on line 61
array(2) {
[0]=>
int(0)
[1]=>
string(8) "variable"
}
array(2) {
[0]=>
string(7) "totally"
[1]=>
string(8) "iterable"
}