xref: /PHP-8.2/ext/spl/tests/fixedarray_003.phpt (revision e9e06279)
1--TEST--
2SPL: FixedArray: Non integer offset handling
3--FILE--
4<?php
5$o = new SplFixedArray(10);
6$r = fopen('php://memory', 'r+');
7
8
9echo 'Write context', \PHP_EOL;
10$o[false] = 'a';
11$o[true] = 'b';
12$o[2.5] = 'c';
13
14try {
15    $o[[]] = 'd';
16} catch (\TypeError $e) {
17    echo $e->getMessage(), "\n";
18}
19try {
20    $o[new stdClass()] = 'e';
21} catch (\TypeError $e) {
22    echo $e->getMessage(), "\n";
23}
24try {
25    $o[$r] = 'f';
26} catch (\TypeError $e) {
27    echo $e->getMessage(), "\n";
28}
29
30$o['3'] = 'g';
31
32try {
33    $o['3.5'] = 'h';
34} catch (\TypeError $e) {
35    echo $e->getMessage(), "\n";
36}
37try {
38    $o['03'] = 'i';
39} catch (\TypeError $e) {
40    echo $e->getMessage(), "\n";
41}
42try {
43    $o[' 3'] = 'j';
44} catch (\TypeError $e) {
45    echo $e->getMessage(), "\n";
46}
47
48
49echo 'Read context', \PHP_EOL;
50var_dump($o[false]);
51var_dump($o[true]);
52var_dump($o[2.5]);
53
54try {
55    var_dump($o[[]]);
56} catch (\TypeError $e) {
57    echo $e->getMessage(), "\n";
58}
59try {
60    var_dump($o[new stdClass()]);
61} catch (\TypeError $e) {
62    echo $e->getMessage(), "\n";
63}
64try {
65    var_dump($o[$r]);
66} catch (\TypeError $e) {
67    echo $e->getMessage(), "\n";
68}
69
70var_dump($o['3']);
71
72try {
73    var_dump($o['3.5']);
74} catch (\TypeError $e) {
75    echo $e->getMessage(), "\n";
76}
77try {
78    var_dump($o['03']);
79} catch (\TypeError $e) {
80    echo $e->getMessage(), "\n";
81}
82try {
83    var_dump($o[' 3']);
84} catch (\TypeError $e) {
85    echo $e->getMessage(), "\n";
86}
87
88
89echo 'isset()', \PHP_EOL;
90var_dump(isset($o[false]));
91var_dump(isset($o[true]));
92var_dump(isset($o[2.5]));
93
94try {
95    var_dump(isset($o[[]]));
96} catch (\TypeError $e) {
97    echo $e->getMessage(), "\n";
98}
99try {
100    var_dump(isset($o[new stdClass()]));
101} catch (\TypeError $e) {
102    echo $e->getMessage(), "\n";
103}
104try {
105    var_dump(isset($o[$r]));
106} catch (\TypeError $e) {
107    echo $e->getMessage(), "\n";
108}
109
110var_dump(isset($o['3']));
111
112try {
113    var_dump(isset($o['3.5']));
114} catch (\TypeError $e) {
115    echo $e->getMessage(), "\n";
116}
117try {
118    var_dump(isset($o['03']));
119} catch (\TypeError $e) {
120    echo $e->getMessage(), "\n";
121}
122try {
123    var_dump(isset($o[' 3']));
124} catch (\TypeError $e) {
125    echo $e->getMessage(), "\n";
126}
127
128echo 'empty()', \PHP_EOL;
129var_dump(empty($o[false]));
130var_dump(empty($o[true]));
131var_dump(empty($o[2.5]));
132
133try {
134    var_dump(empty($o[[]]));
135} catch (\TypeError $e) {
136    echo $e->getMessage(), "\n";
137}
138try {
139    var_dump(empty($o[new stdClass()]));
140} catch (\TypeError $e) {
141    echo $e->getMessage(), "\n";
142}
143try {
144    var_dump(empty($o[$r]));
145} catch (\TypeError $e) {
146    echo $e->getMessage(), "\n";
147}
148
149var_dump(empty($o['3']));
150
151try {
152    var_dump(empty($o['3.5']));
153} catch (\TypeError $e) {
154    echo $e->getMessage(), "\n";
155}
156try {
157    var_dump(empty($o['03']));
158} catch (\TypeError $e) {
159    echo $e->getMessage(), "\n";
160}
161try {
162    var_dump(empty($o[' 3']));
163} catch (\TypeError $e) {
164    echo $e->getMessage(), "\n";
165}
166?>
167--EXPECTF--
168Write context
169
170Deprecated: Implicit conversion from float 2.5 to int loses precision in %s on line %d
171Illegal offset type
172Illegal offset type
173
174Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
175Illegal offset type
176Illegal offset type
177Illegal offset type
178Read context
179string(1) "a"
180string(1) "b"
181
182Deprecated: Implicit conversion from float 2.5 to int loses precision in %s on line %d
183string(1) "c"
184Illegal offset type
185Illegal offset type
186
187Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
188string(1) "f"
189string(1) "g"
190Illegal offset type
191Illegal offset type
192Illegal offset type
193isset()
194bool(true)
195bool(true)
196
197Deprecated: Implicit conversion from float 2.5 to int loses precision in %s on line %d
198bool(true)
199Illegal offset type
200Illegal offset type
201
202Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
203bool(true)
204bool(true)
205Illegal offset type
206Illegal offset type
207Illegal offset type
208empty()
209bool(false)
210bool(false)
211
212Deprecated: Implicit conversion from float 2.5 to int loses precision in %s on line %d
213bool(false)
214Illegal offset type
215Illegal offset type
216
217Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
218bool(false)
219bool(false)
220Illegal offset type
221Illegal offset type
222Illegal offset type
223