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