bug63217.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. --TEST--
  2. Bug #63217 (Constant numeric strings become integers when used as ArrayAccess offset)
  3. --INI--
  4. opcache.enable_cli=1
  5. opcache.enable=1
  6. opcache.optimization_level=-1
  7. --FILE--
  8. <?php
  9. class 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. $test = new Test;
  30. /* These should all produce string(...) "..." output and not int(...) */
  31. isset($test['0']);
  32. isset($test['123']);
  33. unset($test['0']);
  34. unset($test['123']);
  35. $test['0'] = true;
  36. $test['123'] = true;
  37. $foo = $test['0'];
  38. $foo = $test['123'];
  39. /* These caused the same bug, but in opcache rather than the compiler */
  40. isset($test[(string)'0']);
  41. isset($test[(string)'123']);
  42. unset($test[(string)'0']);
  43. unset($test[(string)'123']);
  44. $test[(string)'0'] = true;
  45. $test[(string)'123'] = true;
  46. $foo = $test[(string)'0'];
  47. $foo = $test[(string)'123'];
  48. /**
  49. * @see https://github.com/php/php-src/pull/2607#issuecomment-313781748
  50. */
  51. function test(): string {
  52. $array["10"] = 42;
  53. foreach ($array as $key => $value) {
  54. return $key;
  55. }
  56. }
  57. var_dump(test());
  58. /**
  59. * Make sure we don't break arrays.
  60. */
  61. $array = [];
  62. $key = '123';
  63. $array[$key] = 1;
  64. $array['321'] = 2;
  65. $array['abc'] = 3;
  66. var_dump($array);
  67. /**
  68. * Make sure that we haven't broken ArrayObject
  69. */
  70. $ao = new ArrayObject();
  71. $key = '123';
  72. $ao = [];
  73. $ao[$key] = 1;
  74. $ao['321'] = 2;
  75. $ao['abc'] = 3;
  76. var_dump($ao);
  77. ?>
  78. --EXPECT--
  79. offsetExists given string(1) "0"
  80. offsetExists given string(3) "123"
  81. offsetUnset given string(1) "0"
  82. offsetUnset given string(3) "123"
  83. offsetSet given string(1) "0"
  84. offsetSet given string(3) "123"
  85. offsetGet given string(1) "0"
  86. offsetGet given string(3) "123"
  87. offsetExists given string(1) "0"
  88. offsetExists given string(3) "123"
  89. offsetUnset given string(1) "0"
  90. offsetUnset given string(3) "123"
  91. offsetSet given string(1) "0"
  92. offsetSet given string(3) "123"
  93. offsetGet given string(1) "0"
  94. offsetGet given string(3) "123"
  95. string(2) "10"
  96. array(3) {
  97. [123]=>
  98. int(1)
  99. [321]=>
  100. int(2)
  101. ["abc"]=>
  102. int(3)
  103. }
  104. array(3) {
  105. [123]=>
  106. int(1)
  107. [321]=>
  108. int(2)
  109. ["abc"]=>
  110. int(3)
  111. }