xref: /PHP-8.0/ext/spl/tests/bug31185.phpt (revision f8d79582)
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) {
10        return isset($this->array[$index]);
11    }
12
13    public function offsetGet($index) {
14        return $this->array[$index];
15    }
16
17    public function offsetSet($index, $value) {
18        echo __METHOD__ . "($index, $value)\n";
19        $this->array[$index] = $value;
20    }
21
22    public function offsetUnset($index) {
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