bug71731.phpt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --TEST--
  2. Bug #71731: Null coalescing operator and ArrayAccess
  3. --FILE--
  4. <?php
  5. class AA implements ArrayAccess {
  6. private $data = [];
  7. public function offsetExists($name): bool {
  8. echo "offsetExists($name)\n";
  9. return array_key_exists($name, $this->data);
  10. }
  11. public function &offsetGet($name): mixed {
  12. echo "offsetGet($name)\n";
  13. if (!array_key_exists($name, $this->data)) {
  14. throw new Exception('Unknown offset');
  15. }
  16. return $this->data[$name];
  17. }
  18. public function offsetSet($name, $value): void {
  19. echo "offsetSet($name)\n";
  20. $this->data[$name] = $value;
  21. }
  22. public function offsetUnset($name): void {
  23. echo "offsetUnset($name)\n";
  24. unset($this->data[$name]);
  25. }
  26. }
  27. $aa = new AA;
  28. var_dump(isset($aa[0][1][2]));
  29. var_dump(isset($aa[0]->foo));
  30. var_dump($aa[0] ?? 42);
  31. var_dump($aa[0][1][2] ?? 42);
  32. $aa[0] = new AA;
  33. $aa[0][1] = new AA;
  34. var_dump(isset($aa[0][1][2]));
  35. var_dump($aa[0][1][2] ?? 42);
  36. ?>
  37. --EXPECT--
  38. offsetExists(0)
  39. bool(false)
  40. offsetExists(0)
  41. bool(false)
  42. offsetExists(0)
  43. int(42)
  44. offsetExists(0)
  45. int(42)
  46. offsetSet(0)
  47. offsetGet(0)
  48. offsetSet(1)
  49. offsetExists(0)
  50. offsetGet(0)
  51. offsetExists(1)
  52. offsetGet(1)
  53. offsetExists(2)
  54. bool(false)
  55. offsetExists(0)
  56. offsetGet(0)
  57. offsetExists(1)
  58. offsetGet(1)
  59. offsetExists(2)
  60. int(42)