3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ArrayStorage implements Iterator, ArrayAccess { private $holder = []; private $instanceName; public function __construct($instanceName) { if (!class_exists($instanceName)) { throw new \Exception('Class '.$instanceName.' was not found'); } $this->instanceName = $instanceName; } public function rewind() { reset($this->holder); } public function current() { return current($this->holder); } public function key() { return key($this->holder); } public function next() { next($this->holder); } public function valid() { return false !== $this->current(); } public function offsetSet($offset, $value) { if (!($value instanceof $this->instanceName)) { throw new \Exception('Storage allows only '.$this->instanceName.' instances'); } if (is_null($offset)) { $this->holder[] = $value; } else { $this->holder[$offset] = $value; } } public function offsetExists($offset) { return isset($this->holder[$offset]); } public function offsetUnset($offset) { unset($this->holder[$offset]); } public function offsetGet($offset) { return isset($this->holder[$offset]) ? $this->holder[$offset] : null; } } class Foo {} class Bar {} $storage = new ArrayStorage('Foo'); $storage[] = new Foo; $storage['baz'] = new Foo; //var_export($storage); foreach ($storage as $key => $value) { echo($key. ' => '.PHP_EOL.var_export($value, 1).PHP_EOL); } $storage['bee'] = new Bar;
Output for 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Deprecated: Return type of ArrayStorage::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 21 Deprecated: Return type of ArrayStorage::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 31 Deprecated: Return type of ArrayStorage::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 26 Deprecated: Return type of ArrayStorage::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 36 Deprecated: Return type of ArrayStorage::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 16 Deprecated: Return type of ArrayStorage::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/4jWtD on line 53 Deprecated: Return type of ArrayStorage::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/4jWtD on line 63 Deprecated: Return type of ArrayStorage::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/4jWtD on line 41 Deprecated: Return type of ArrayStorage::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/4jWtD on line 58 0 => \Foo::__set_state(array( )) baz => \Foo::__set_state(array( )) Fatal error: Uncaught Exception: Storage allows only Foo instances in /in/4jWtD:44 Stack trace: #0 /in/4jWtD(80): ArrayStorage->offsetSet('bee', Object(Bar)) #1 {main} thrown in /in/4jWtD on line 44
Process exited with code 255.
Output for 8.1.0 - 8.1.28
Deprecated: Return type of ArrayStorage::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 21 Deprecated: Return type of ArrayStorage::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 31 Deprecated: Return type of ArrayStorage::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 26 Deprecated: Return type of ArrayStorage::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 36 Deprecated: Return type of ArrayStorage::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4jWtD on line 16 Deprecated: Return type of ArrayStorage::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/4jWtD on line 53 Deprecated: Return type of ArrayStorage::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/4jWtD on line 63 Deprecated: Return type of ArrayStorage::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/4jWtD on line 41 Deprecated: Return type of ArrayStorage::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/4jWtD on line 58 0 => Foo::__set_state(array( )) baz => Foo::__set_state(array( )) Fatal error: Uncaught Exception: Storage allows only Foo instances in /in/4jWtD:44 Stack trace: #0 /in/4jWtD(80): ArrayStorage->offsetSet('bee', Object(Bar)) #1 {main} thrown in /in/4jWtD on line 44
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
0 => Foo::__set_state(array( )) baz => Foo::__set_state(array( )) Fatal error: Uncaught Exception: Storage allows only Foo instances in /in/4jWtD:44 Stack trace: #0 /in/4jWtD(80): ArrayStorage->offsetSet('bee', Object(Bar)) #1 {main} thrown in /in/4jWtD on line 44
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
0 => Foo::__set_state(array( )) baz => Foo::__set_state(array( )) Fatal error: Uncaught exception 'Exception' with message 'Storage allows only Foo instances' in /in/4jWtD:44 Stack trace: #0 /in/4jWtD(80): ArrayStorage->offsetSet('bee', Object(Bar)) #1 {main} thrown in /in/4jWtD on line 44
Process exited with code 255.
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 '[' in /in/4jWtD on line 5
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/4jWtD on line 5
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/4jWtD on line 3
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/4jWtD on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/4jWtD on line 3
Process exited with code 255.

preferences:
246.15 ms | 401 KiB | 359 Q