1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
--TEST--
Coalesce assign (??=): ArrayAccess handling
--FILE--
<?php
function id($arg) {
echo "id($arg)\n";
return $arg;
}
class AA implements ArrayAccess {
public $data;
public function __construct($data = []) {
$this->data = $data;
}
public function &offsetGet($k): mixed {
echo "offsetGet($k)\n";
return $this->data[$k];
}
public function offsetExists($k): bool {
echo "offsetExists($k)\n";
return array_key_exists($k, $this->data);
}
public function offsetSet($k,$v): void {
echo "offsetSet($k,$v)\n";
$this->data[$k] = $v;
}
public function offsetUnset($k): void { }
}
$ary = new AA(["foo" => new AA, "null" => null]);
echo "[foo]\n";
$ary["foo"] ??= "bar";
echo "[bar]\n";
$ary["bar"] ??= "foo";
echo "[null]\n";
$ary["null"] ??= "baz";
echo "[foo][bar]\n";
$ary["foo"]["bar"] ??= "abc";
echo "[foo][bar]\n";
$ary["foo"]["bar"] ??= "def";
?>
--EXPECT--
[foo]
offsetExists(foo)
offsetGet(foo)
[bar]
offsetExists(bar)
offsetSet(bar,foo)
[null]
offsetExists(null)
offsetGet(null)
offsetSet(null,baz)
[foo][bar]
offsetExists(foo)
offsetGet(foo)
offsetExists(bar)
offsetGet(foo)
offsetSet(bar,abc)
[foo][bar]
offsetExists(foo)
offsetGet(foo)
offsetExists(bar)
offsetGet(bar)
|