3v4l.org

run code in 300+ PHP versions simultaneously
<?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 . ' got ' . get_class($newValue)); } } } 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); } } */
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of TypedArray::offsetSet($index, $newValue) should either be compatible with ArrayIterator::offsetSet(mixed $key, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/6ZHCZ on line 14 Fatal error: Uncaught TypeError: get_class(): Argument #1 ($object) must be of type object, string given in /in/6ZHCZ:17 Stack trace: #0 /in/6ZHCZ(34): TypedArray->offsetSet(NULL, 'blah') #1 {main} thrown in /in/6ZHCZ on line 17
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
51.31 ms | 401 KiB | 8 Q