xref: /PHP-8.1/Zend/tests/bug63217.phpt (revision 75a678a7)
1--TEST--
2Bug #63217 (Constant numeric strings become integers when used as ArrayAccess offset)
3--INI--
4opcache.enable_cli=1
5opcache.enable=1
6opcache.optimization_level=-1
7--FILE--
8<?php
9class Test implements ArrayAccess {
10    public function offsetExists($offset): bool {
11        echo "offsetExists given ";
12        var_dump($offset);
13        return true;
14    }
15    public function offsetUnset($offset): void {
16        echo "offsetUnset given ";
17        var_dump($offset);
18    }
19    public function offsetSet($offset, $value): void {
20        echo "offsetSet given ";
21        var_dump($offset);
22    }
23    public function offsetGet($offset): mixed {
24        echo "offsetGet given ";
25        var_dump($offset);
26        return null;
27    }
28}
29
30$test = new Test;
31
32/* These should all produce string(...) "..." output and not int(...) */
33isset($test['0']);
34isset($test['123']);
35unset($test['0']);
36unset($test['123']);
37$test['0'] = true;
38$test['123'] = true;
39$foo = $test['0'];
40$foo = $test['123'];
41
42/* These caused the same bug, but in opcache rather than the compiler */
43isset($test[(string)'0']);
44isset($test[(string)'123']);
45unset($test[(string)'0']);
46unset($test[(string)'123']);
47$test[(string)'0'] = true;
48$test[(string)'123'] = true;
49$foo = $test[(string)'0'];
50$foo = $test[(string)'123'];
51
52/**
53 * @see https://github.com/php/php-src/pull/2607#issuecomment-313781748
54 */
55function test(): string {
56    $array["10"] = 42;
57    foreach ($array as $key => $value) {
58        return $key;
59    }
60}
61
62var_dump(test());
63
64/**
65 * Make sure we don't break arrays.
66 */
67$array = [];
68
69$key = '123';
70
71$array[$key] = 1;
72$array['321'] = 2;
73$array['abc'] = 3;
74
75var_dump($array);
76
77/**
78 * Make sure that we haven't broken ArrayObject
79 */
80$ao = new ArrayObject();
81
82$key = '123';
83
84$ao = [];
85$ao[$key] = 1;
86$ao['321'] = 2;
87$ao['abc'] = 3;
88
89var_dump($ao);
90
91?>
92--EXPECT--
93offsetExists given string(1) "0"
94offsetExists given string(3) "123"
95offsetUnset given string(1) "0"
96offsetUnset given string(3) "123"
97offsetSet given string(1) "0"
98offsetSet given string(3) "123"
99offsetGet given string(1) "0"
100offsetGet given string(3) "123"
101offsetExists given string(1) "0"
102offsetExists given string(3) "123"
103offsetUnset given string(1) "0"
104offsetUnset given string(3) "123"
105offsetSet given string(1) "0"
106offsetSet given string(3) "123"
107offsetGet given string(1) "0"
108offsetGet given string(3) "123"
109string(2) "10"
110array(3) {
111  [123]=>
112  int(1)
113  [321]=>
114  int(2)
115  ["abc"]=>
116  int(3)
117}
118array(3) {
119  [123]=>
120  int(1)
121  [321]=>
122  int(2)
123  ["abc"]=>
124  int(3)
125}
126