3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 ... ?>
based on iV3cF
Output for 7.0.0 - 7.2.0
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.6.21
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/Fa5Tk on line 2
Process exited with code 255.
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected T_CLASS in /in/Fa5Tk on line 2
Process exited with code 255.