xref: /PHP-8.1/ext/spl/tests/bug31185.phpt (revision c6357b80)
1--TEST--
2Bug #31185 (Crash when exceptions thrown from ArrayAccess::offsetUnset())
3--FILE--
4<?php
5
6class FooBar implements ArrayAccess {
7    private $array = array();
8
9    public function offsetExists($index): bool {
10        return isset($this->array[$index]);
11    }
12
13    public function offsetGet($index): mixed {
14        return $this->array[$index];
15    }
16
17    public function offsetSet($index, $value): void {
18        echo __METHOD__ . "($index, $value)\n";
19        $this->array[$index] = $value;
20    }
21
22    public function offsetUnset($index): void {
23        throw new Exception('FAIL');
24        unset($this->array[$index]);
25    }
26
27}
28
29$i = 0; $j = 0;
30$foo = new FooBar();
31$foo[$j++] = $i++;
32$foo[$j++] = $i++;
33$foo[$j++] = $i++;
34try
35{
36    unset($foo[1]);
37}
38catch (Exception $e)
39{
40    echo "CAUGHT: " . $e->getMessage() . "\n";
41}
42
43print_R($foo);
44?>
45--EXPECT--
46FooBar::offsetSet(0, 0)
47FooBar::offsetSet(1, 1)
48FooBar::offsetSet(2, 2)
49CAUGHT: FAIL
50FooBar Object
51(
52    [array:FooBar:private] => Array
53        (
54            [0] => 0
55            [1] => 1
56            [2] => 2
57        )
58
59)
60