3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php final class TypedArray extends ArrayIterator { private $class; public function __construct($class) { $this->class = $class; parent::__construct(array()); } public function offsetSet($index, $newValue) { if (!$newValue instanceof $this->class) { throw new \InvalidArgumentException('Expected instance of ' . $this->class); } } } class A {} $array = new TypedArray(A::class); $array[] = new A; $array['boo'] = new A; foreach ($array as $key => $value) { echo $key . ' -> '; var_dump($value); echo PHP_EOL; } $array[] = 'blah'; /* implements Iterator, ArrayAccess { private $class; private $array; public function __construct($class) { $this->class = $class; $this->array = array(); } public function current() { return current($this->array); } public function key() { return key($this->array); } public function next() { return next($this->array); } public function rewind() { return rewind($this->array); } } */
based on 6ZHCZ
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught InvalidArgumentException: Expected instance of A in /in/TWXI2:17 Stack trace: #0 /in/TWXI2(34): TypedArray->offsetSet(NULL, 'blah') #1 {main} thrown in /in/TWXI2 on line 17
Process exited with code 255.
Output for 5.5.24 - 5.6.28
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Expected instance of A' in /in/TWXI2:17 Stack trace: #0 /in/TWXI2(34): TypedArray->offsetSet(NULL, 'blah') #1 {main} thrown in /in/TWXI2 on line 17
Process exited with code 255.
Output for 5.4.12 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/TWXI2 on line 24
Process exited with code 255.
Output for 5.3.22 - 5.3.29
Parse error: syntax error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /in/TWXI2 on line 24
Process exited with code 255.