xref: /PHP-8.3/Zend/tests/bug71731.phpt (revision 75a678a7)
1--TEST--
2Bug #71731: Null coalescing operator and ArrayAccess
3--FILE--
4<?php
5
6class AA implements ArrayAccess {
7    private $data = [];
8    public function offsetExists($name): bool {
9        echo "offsetExists($name)\n";
10        return array_key_exists($name, $this->data);
11    }
12    public function &offsetGet($name): mixed {
13        echo "offsetGet($name)\n";
14        if (!array_key_exists($name, $this->data)) {
15            throw new Exception('Unknown offset');
16        }
17        return $this->data[$name];
18    }
19    public function offsetSet($name, $value): void {
20        echo "offsetSet($name)\n";
21        $this->data[$name] = $value;
22    }
23    public function offsetUnset($name): void {
24        echo "offsetUnset($name)\n";
25        unset($this->data[$name]);
26    }
27}
28
29$aa = new AA;
30var_dump(isset($aa[0][1][2]));
31var_dump(isset($aa[0]->foo));
32var_dump($aa[0] ?? 42);
33var_dump($aa[0][1][2] ?? 42);
34
35$aa[0] = new AA;
36$aa[0][1] = new AA;
37var_dump(isset($aa[0][1][2]));
38var_dump($aa[0][1][2] ?? 42);
39
40?>
41--EXPECT--
42offsetExists(0)
43bool(false)
44offsetExists(0)
45bool(false)
46offsetExists(0)
47int(42)
48offsetExists(0)
49int(42)
50offsetSet(0)
51offsetGet(0)
52offsetSet(1)
53offsetExists(0)
54offsetGet(0)
55offsetExists(1)
56offsetGet(1)
57offsetExists(2)
58bool(false)
59offsetExists(0)
60offsetGet(0)
61offsetExists(1)
62offsetGet(1)
63offsetExists(2)
64int(42)
65