3v4l.org

run code in 300+ PHP versions simultaneously
<?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]); }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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" }
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
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" }
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/AGvhG on line 71
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[', expecting ')' in /in/AGvhG on line 71
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/AGvhG on line 2
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
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/AGvhG on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/AGvhG on line 2
Process exited with code 255.

preferences:
204.52 ms | 401 KiB | 356 Q