xref: /php-src/ext/spl/tests/fixedarray_003.phpt (revision 99fa740a)
1--TEST--
2SPL: SplFixedArray: 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
171Cannot access offset of type array on SplFixedArray
172Cannot access offset of type stdClass on SplFixedArray
173
174Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
175Cannot access offset of type string on SplFixedArray
176Cannot access offset of type string on SplFixedArray
177Cannot access offset of type string on SplFixedArray
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"
184Cannot access offset of type array on SplFixedArray
185Cannot access offset of type stdClass on SplFixedArray
186
187Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
188string(1) "f"
189string(1) "g"
190Cannot access offset of type string on SplFixedArray
191Cannot access offset of type string on SplFixedArray
192Cannot access offset of type string on SplFixedArray
193isset()
194bool(true)
195bool(true)
196
197Deprecated: Implicit conversion from float 2.5 to int loses precision in %s on line %d
198bool(true)
199Cannot access offset of type array on SplFixedArray
200Cannot access offset of type stdClass on SplFixedArray
201
202Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
203bool(true)
204bool(true)
205Cannot access offset of type string on SplFixedArray
206Cannot access offset of type string on SplFixedArray
207Cannot access offset of type string on SplFixedArray
208empty()
209bool(false)
210bool(false)
211
212Deprecated: Implicit conversion from float 2.5 to int loses precision in %s on line %d
213bool(false)
214Cannot access offset of type array on SplFixedArray
215Cannot access offset of type stdClass on SplFixedArray
216
217Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
218bool(false)
219bool(false)
220Cannot access offset of type string on SplFixedArray
221Cannot access offset of type string on SplFixedArray
222Cannot access offset of type string on SplFixedArray
223