xref: /PHP-8.4/ext/spl/tests/bug66834.phpt (revision c6357b80)
1--TEST--
2SPL: Bug #66834
3--FILE--
4<?php
5
6// overrides both offsetExists and offsetGet
7class ArrayObjectBoth extends ArrayObject
8{
9    public function offsetExists($offset): bool {
10        var_dump('Called: '.__METHOD__);
11        return parent::offsetExists($offset);
12    }
13
14    public function offsetGet($offset): mixed {
15        var_dump('Called: '.__METHOD__);
16        return parent::offsetGet($offset);
17    }
18}
19
20// overrides only offsetExists
21class ArrayObjectExists extends ArrayObject
22{
23    public function offsetExists($offset): bool {
24        var_dump('Called: '.__METHOD__);
25        return parent::offsetExists($offset);
26    }
27}
28
29// overrides only offsetGet
30class ArrayObjectGet extends ArrayObject
31{
32    public function offsetGet($offset): mixed {
33        var_dump('Called: '.__METHOD__);
34        return parent::offsetGet($offset);
35    }
36}
37
38// overrides only offsetGet and offsetSet
39class ArrayObjectGetSet extends ArrayObject
40{
41    public function offsetGet($offset): mixed
42    {
43        return parent::offsetGet(str_rot13($offset));
44    }
45
46    public function offsetSet($offset, $value): void
47    {
48        parent::offsetSet(str_rot13($offset), $value);
49    }
50}
51
52$values = ['foo' => '', 'bar' => null, 'baz' => 42];
53
54echo "==== class with offsetExists() and offsetGet() ====\n";
55$object = new ArrayObjectBoth($values);
56var_dump($object->offsetExists('foo'), isset($object['foo']), empty($object['foo']));
57var_dump($object->offsetExists('bar'), isset($object['bar']), empty($object['bar']));
58var_dump($object->offsetexists('baz'), isset($object['baz']), empty($object['baz']));
59var_dump($object->offsetexists('qux'), isset($object['qux']), empty($object['qux']));
60
61echo "==== class with offsetExists() ====\n";
62$object = new ArrayObjectExists($values);
63var_dump($object->offsetExists('foo'), isset($object['foo']), empty($object['foo']));
64var_dump($object->offsetExists('bar'), isset($object['bar']), empty($object['bar']));
65var_dump($object->offsetexists('baz'), isset($object['baz']), empty($object['baz']));
66var_dump($object->offsetexists('qux'), isset($object['qux']), empty($object['qux']));
67
68echo "==== class with offsetGet() ====\n";
69$object = new ArrayObjectGet($values);
70var_dump($object->offsetExists('foo'), isset($object['foo']), empty($object['foo']));
71var_dump($object->offsetExists('bar'), isset($object['bar']), empty($object['bar']));
72var_dump($object->offsetexists('baz'), isset($object['baz']), empty($object['baz']));
73var_dump($object->offsetexists('qux'), isset($object['qux']), empty($object['qux']));
74
75echo "==== class with offsetGet() and offsetSet() ====\n";
76$object = new ArrayObjectGetSet;
77$object['foo'] = 42;
78var_dump($object->offsetExists('foo'), $object->offsetExists('sbb'), isset($object['foo']), isset($object['sbb']), empty($object['sbb']));
79
80?>
81--EXPECTF--
82==== class with offsetExists() and offsetGet() ====
83string(37) "Called: ArrayObjectBoth::offsetExists"
84string(37) "Called: ArrayObjectBoth::offsetExists"
85string(37) "Called: ArrayObjectBoth::offsetExists"
86string(34) "Called: ArrayObjectBoth::offsetGet"
87bool(true)
88bool(true)
89bool(true)
90string(37) "Called: ArrayObjectBoth::offsetExists"
91string(37) "Called: ArrayObjectBoth::offsetExists"
92string(37) "Called: ArrayObjectBoth::offsetExists"
93string(34) "Called: ArrayObjectBoth::offsetGet"
94bool(true)
95bool(true)
96bool(true)
97string(37) "Called: ArrayObjectBoth::offsetExists"
98string(37) "Called: ArrayObjectBoth::offsetExists"
99string(37) "Called: ArrayObjectBoth::offsetExists"
100string(34) "Called: ArrayObjectBoth::offsetGet"
101bool(true)
102bool(true)
103bool(false)
104string(37) "Called: ArrayObjectBoth::offsetExists"
105string(37) "Called: ArrayObjectBoth::offsetExists"
106string(37) "Called: ArrayObjectBoth::offsetExists"
107bool(false)
108bool(false)
109bool(true)
110==== class with offsetExists() ====
111string(39) "Called: ArrayObjectExists::offsetExists"
112string(39) "Called: ArrayObjectExists::offsetExists"
113string(39) "Called: ArrayObjectExists::offsetExists"
114bool(true)
115bool(true)
116bool(true)
117string(39) "Called: ArrayObjectExists::offsetExists"
118string(39) "Called: ArrayObjectExists::offsetExists"
119string(39) "Called: ArrayObjectExists::offsetExists"
120bool(true)
121bool(true)
122bool(true)
123string(39) "Called: ArrayObjectExists::offsetExists"
124string(39) "Called: ArrayObjectExists::offsetExists"
125string(39) "Called: ArrayObjectExists::offsetExists"
126bool(true)
127bool(true)
128bool(false)
129string(39) "Called: ArrayObjectExists::offsetExists"
130string(39) "Called: ArrayObjectExists::offsetExists"
131string(39) "Called: ArrayObjectExists::offsetExists"
132bool(false)
133bool(false)
134bool(true)
135==== class with offsetGet() ====
136string(33) "Called: ArrayObjectGet::offsetGet"
137bool(true)
138bool(true)
139bool(true)
140string(33) "Called: ArrayObjectGet::offsetGet"
141bool(true)
142bool(false)
143bool(true)
144string(33) "Called: ArrayObjectGet::offsetGet"
145bool(true)
146bool(true)
147bool(false)
148bool(false)
149bool(false)
150bool(true)
151==== class with offsetGet() and offsetSet() ====
152
153Warning: Undefined array key "foo" in %s on line %d
154bool(false)
155bool(true)
156bool(false)
157bool(true)
158bool(true)
159