1--TEST--
2ZE2 ArrayAccess and sub Arrays
3--FILE--
4<?php
5
6class Peoples implements ArrayAccess {
7    public $person;
8
9    function __construct() {
10        $this->person = array(array('name'=>'Joe'));
11    }
12
13    function offsetExists($index): bool {
14        return array_key_exists($this->person, $index);
15    }
16
17    function offsetGet($index): mixed {
18        return $this->person[$index];
19    }
20
21    function offsetSet($index, $value): void {
22        $this->person[$index] = $value;
23    }
24
25    function offsetUnset($index): void {
26        unset($this->person[$index]);
27    }
28}
29
30$people = new Peoples;
31
32var_dump($people->person[0]['name']);
33$people->person[0]['name'] = $people->person[0]['name'] . 'Foo';
34var_dump($people->person[0]['name']);
35$people->person[0]['name'] .= 'Bar';
36var_dump($people->person[0]['name']);
37
38echo "---ArrayOverloading---\n";
39
40$people = new Peoples;
41
42var_dump($people[0]);
43var_dump($people[0]['name']);
44var_dump($people->person[0]['name'] . 'Foo'); // impossible to assign this since we don't return references here
45$x = $people[0]; // creates a copy
46$x['name'] .= 'Foo';
47$people[0] = $x;
48var_dump($people[0]);
49$people[0]['name'] = 'JoeFoo';
50var_dump($people[0]['name']);
51$people[0]['name'] = 'JoeFooBar';
52var_dump($people[0]['name']);
53
54?>
55--EXPECTF--
56string(3) "Joe"
57string(6) "JoeFoo"
58string(9) "JoeFooBar"
59---ArrayOverloading---
60array(1) {
61  ["name"]=>
62  string(3) "Joe"
63}
64string(3) "Joe"
65string(6) "JoeFoo"
66array(1) {
67  ["name"]=>
68  string(6) "JoeFoo"
69}
70
71Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_005.php on line 46
72string(6) "JoeFoo"
73
74Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_005.php on line 48
75string(6) "JoeFoo"
76