<?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 ...
?>
preferences:
36.85 ms | 402 KiB | 5 Q