xref: /php-src/ext/spl/tests/gh10519.phpt (revision 49b2ff5d)
1--TEST--
2Bug GH-10519 (Array Data Address Reference Issue)
3--FILE--
4<?php
5interface DataInterface extends JsonSerializable, RecursiveIterator, Iterator
6{
7    public static function init(Traversable $data): DataInterface;
8}
9
10class A extends RecursiveArrayIterator implements DataInterface
11{
12    public function __construct($data)
13    {
14        parent::__construct($data);
15    }
16
17    public static function init($data): DataInterface
18    {
19        return new static($data);
20    }
21
22    public function getCols(string $colname): array
23    {
24        return array_column($this->getArrayCopy(), $colname);
25    }
26
27    public function bugySetMethod($key, $value)
28    {
29        $data = &$this;
30
31        while ($data->hasChildren()) {
32            $data = $data->getChildren();
33        }
34	    $data->offsetSet($key, $value);
35    }
36
37    public function jsonSerialize(): mixed
38    {
39        return $this;
40    }
41}
42
43$a = [
44    'test' => [
45        'a' => (object) [2 => '',3 => '',4 => ''],
46    ]
47];
48
49$example = A::init($a);
50$example->bugySetMethod(5, 'in here');
51var_dump(json_encode($example));
52var_dump(json_encode($a));
53
54$b = [
55    'test' => [
56        'b' => [2 => '',3 => '',4 => ''],
57    ]
58];
59$example = A::init($b);
60
61$example->bugySetMethod(5, 'must be here');
62var_dump(json_encode($example));
63var_dump(json_encode($b));
64?>
65--EXPECT--
66string(51) "{"test":{"a":{"2":"","3":"","4":"","5":"in here"}}}"
67string(51) "{"test":{"a":{"2":"","3":"","4":"","5":"in here"}}}"
68string(56) "{"test":{"b":{"2":"","3":"","4":"","5":"must be here"}}}"
69string(37) "{"test":{"b":{"2":"","3":"","4":""}}}"
70