3v4l.org

run code in 300+ PHP versions simultaneously
<?php $config = new class([ "item" => [ "I", "am", "a", "animal" => "beaver", ], "callable" => function(){ return true; } ]) implements ArrayAccess { public function __construct(array $initial = []) { foreach ($initial as $k => $v) { if (is_array($v)) { $this->$k = new self($v); } else $this->$k = $v; } } public function offsetGet($offset) { if (!isset($this->$offset)) { return ($this->$offset = new self()); } return $this->$offset; } public function offsetSet($offset, $value) { if (is_array($value)) { $this->$offset = new self($value); } else $this->$offset = $value; } public function offsetUnset($offset) { unset($this->$offset); } public function offsetExists($offset) { return isset($this->$offset); } public function array(self $object = null) : array { $array = []; if ($object === null) $object = $this; foreach ($object as $k => $v) { if ($v instanceof self) { $array[$k] = $this->array($v); } else $array[$k] = $v; } return $array; } public function __invoke(...$args) {} /* more magic here, perhaps ... */ }; $config["item"]["next"] = [ "I", "am", "a", "animal" => "badger", ]; # write undefined index of existing member $config[1][2] = "hello"; # write undefined index $config[5][8][10] = "whatever"; # write more undef index $config[9][10](); # read undef index and invoke it as function var_dump($config->array()); # show flattened array, not needed for iteration and whatever ... ?>
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 37 Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 22 Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 30 Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 36 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$0 is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$animal is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$item is deprecated in /in/Fa5Tk on line 17 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$callable is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$0 is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$animal is deprecated in /in/Fa5Tk on line 18 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$next is deprecated in /in/Fa5Tk on line 32 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/Fa5Tk on line 24 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/Fa5Tk on line 33 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$5 is deprecated in /in/Fa5Tk on line 24 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$8 is deprecated in /in/Fa5Tk on line 24 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$10 is deprecated in /in/Fa5Tk on line 33 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$9 is deprecated in /in/Fa5Tk on line 24 Deprecated: Creation of dynamic property ArrayAccess@anonymous::$10 is deprecated in /in/Fa5Tk on line 24 array(5) { ["item"]=> array(5) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" ["animal"]=> string(6) "beaver" ["next"]=> array(4) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" ["animal"]=> string(6) "badger" } } ["callable"]=> object(Closure)#2 (0) { } [1]=> array(1) { [2]=> string(5) "hello" } [5]=> array(1) { [8]=> array(1) { [10]=> string(8) "whatever" } } [9]=> array(1) { [10]=> array(0) { } } }
Output for 8.1.0 - 8.1.28
Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 37 Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 22 Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 30 Deprecated: Return type of ArrayAccess@anonymous::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/Fa5Tk on line 36 array(5) { ["item"]=> array(5) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" ["animal"]=> string(6) "beaver" ["next"]=> array(4) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" ["animal"]=> string(6) "badger" } } ["callable"]=> object(Closure)#2 (0) { } [1]=> array(1) { [2]=> string(5) "hello" } [5]=> array(1) { [8]=> array(1) { [10]=> string(8) "whatever" } } [9]=> array(1) { [10]=> array(0) { } } }
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
array(5) { ["item"]=> array(5) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" ["animal"]=> string(6) "beaver" ["next"]=> array(4) { [0]=> string(1) "I" [1]=> string(2) "am" [2]=> string(1) "a" ["animal"]=> string(6) "badger" } } ["callable"]=> object(Closure)#2 (0) { } [1]=> array(1) { [2]=> string(5) "hello" } [5]=> array(1) { [8]=> array(1) { [10]=> string(8) "whatever" } } [9]=> array(1) { [10]=> array(0) { } } }
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.21
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/Fa5Tk on line 2
Process exited with code 255.

preferences:
187.54 ms | 402 KiB | 241 Q