<?php
$nest = new class([
"first" => [
"I",
"am",
"a",
"animal" => "beaver"
]
]) 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 $thing = null) {
$result = [];
if (!$thing)
$thing = $this;
foreach ($thing as $k => $v) {
if ($v instanceof self) {
$result[$k] = $this->array($v);
} else $result[$k] = $v;
}
return $result;
}
public function __invoke(...$args) {}
};
$nest[1][2] = "hello"; # write undefined index
$nest[5][8][10] = "whatever"; # write more undef index
$nest[9][10](); # read undef index and invoke it
var_dump($nest->array()); # show array(s), not needed for iteration and whatever ...
?>
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/iV3cF on line 34
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/iV3cF on line 19
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/iV3cF on line 27
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/iV3cF on line 33
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$0 is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$animal is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$first is deprecated in /in/iV3cF on line 14
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/iV3cF on line 30
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$5 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$8 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$10 is deprecated in /in/iV3cF on line 30
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$9 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$10 is deprecated in /in/iV3cF on line 21
array(4) {
["first"]=>
array(4) {
[0]=>
string(1) "I"
[1]=>
string(2) "am"
[2]=>
string(1) "a"
["animal"]=>
string(6) "beaver"
}
[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.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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/iV3cF on line 34
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/iV3cF on line 19
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/iV3cF on line 27
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/iV3cF on line 33
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$0 is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$animal is deprecated in /in/iV3cF on line 15
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$first is deprecated in /in/iV3cF on line 14
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$1 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$2 is deprecated in /in/iV3cF on line 30
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$5 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$8 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$10 is deprecated in /in/iV3cF on line 30
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$9 is deprecated in /in/iV3cF on line 21
Deprecated: Creation of dynamic property ArrayAccess@anonymous::$10 is deprecated in /in/iV3cF on line 21
array(4) {
["first"]=>
array(4) {
[0]=>
string(1) "I"
[1]=>
string(2) "am"
[2]=>
string(1) "a"
["animal"]=>
string(6) "beaver"
}
[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/iV3cF on line 34
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/iV3cF on line 19
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/iV3cF on line 27
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/iV3cF on line 33
array(4) {
["first"]=>
array(4) {
[0]=>
string(1) "I"
[1]=>
string(2) "am"
[2]=>
string(1) "a"
["animal"]=>
string(6) "beaver"
}
[1]=>
array(1) {
[2]=>
string(5) "hello"
}
[5]=>
array(1) {
[8]=>
array(1) {
[10]=>
string(8) "whatever"
}
}
[9]=>
array(1) {
[10]=>
array(0) {
}
}
}