<?php
class DefaultMap implements ArrayAccess {
private $array;
private $default;
public function __construct($default = null) {
$this->array = [];
$this->default = $default;
}
public function &offsetGet($key) {
if (!isset($this->array[$key])) {
$this->array[$key] = $this->default;
}
return $this->array[$key];
}
public function offsetSet($key, $value) {
$this->array[$key] = $value;
}
public function offsetExists($key) {
return isset($this->array[$key]);
}
public function offsetUnset($key) {
unset($this->array[$key]);
}
public function toArray() {
return $this->array;
}
}
$map = new DefaultMap(0);
$map["foo"]++;
var_dump($map->toArray());
Deprecated: Return type of DefaultMap::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sU57S on line 24
Deprecated: Return type of & DefaultMap::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sU57S on line 12
Deprecated: Return type of DefaultMap::offsetSet($key, $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/sU57S on line 20
Deprecated: Return type of DefaultMap::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sU57S on line 28
array(1) {
["foo"]=>
int(1)
}
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 DefaultMap::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sU57S on line 24
Deprecated: Return type of & DefaultMap::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sU57S on line 12
Deprecated: Return type of DefaultMap::offsetSet($key, $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/sU57S on line 20
Deprecated: Return type of DefaultMap::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sU57S on line 28
array(1) {
["foo"]=>
int(1)
}