xref: /PHP-8.2/Zend/tests/bug40833.phpt (revision 75a678a7)
1--TEST--
2Bug #40833 (Crash when using unset() on an ArrayAccess object retrieved via __get)
3--FILE--
4<?php
5    class entity
6    {
7        private $data;
8        private $modified;
9
10        function __get($name)
11        {
12            if ( isset($this->data[$name]) )
13                return $this->data[$name];
14            else
15                return $this->data[$name] = new set($this);
16        }
17
18        function __set($name, $value)
19        {
20            $this->modified[$name] = $value;
21        }
22    }
23
24    class set implements ArrayAccess
25    {
26        private $entity;
27
28        function __construct($entity)
29        {
30            $this->entity = $entity;
31            $this->entity->whatever = $this;
32        }
33
34        function clear() {
35            $this->entity->whatever = null;
36        }
37
38        function offsetUnset($offset): void
39        {
40            $this->clear();
41//			$this->entity->{$this->name} = null;
42        }
43
44        function offsetSet($offset, $value): void
45        {
46        }
47
48        function offsetGet($offset): mixed
49        {
50            return 'Bogus ';
51        }
52
53        function offsetExists($offset): bool
54        {
55        }
56    }
57
58    $entity = new entity();
59    echo($entity->whatever[0]);
60
61    //This will crash
62//	$entity->whatever->clear();
63    unset($entity->whatever[0]);
64
65    //This will not crash (comment previous & uncomment this to test
66//	$test = $entity->whatever; unset($test[0]);
67
68    echo($entity->whatever[0]);
69    echo "ok\n";
70?>
71--EXPECT--
72Bogus Bogus ok
73