xref: /PHP-7.4/ext/spl/tests/bug31185.phpt (revision 610c7fbe)
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===DONE===
46--EXPECT--
47FooBar::offsetSet(0, 0)
48FooBar::offsetSet(1, 1)
49FooBar::offsetSet(2, 2)
50CAUGHT: FAIL
51FooBar Object
52(
53    [array:FooBar:private] => Array
54        (
55            [0] => 0
56            [1] => 1
57            [2] => 2
58        )
59
60)
61===DONE===
62