assign_coalesce_003.phpt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. --TEST--
  2. Coalesce assign (??=): ArrayAccess handling
  3. --FILE--
  4. <?php
  5. function id($arg) {
  6. echo "id($arg)\n";
  7. return $arg;
  8. }
  9. class AA implements ArrayAccess {
  10. public $data;
  11. public function __construct($data = []) {
  12. $this->data = $data;
  13. }
  14. public function &offsetGet($k): mixed {
  15. echo "offsetGet($k)\n";
  16. return $this->data[$k];
  17. }
  18. public function offsetExists($k): bool {
  19. echo "offsetExists($k)\n";
  20. return array_key_exists($k, $this->data);
  21. }
  22. public function offsetSet($k,$v): void {
  23. echo "offsetSet($k,$v)\n";
  24. $this->data[$k] = $v;
  25. }
  26. public function offsetUnset($k): void { }
  27. }
  28. $ary = new AA(["foo" => new AA, "null" => null]);
  29. echo "[foo]\n";
  30. $ary["foo"] ??= "bar";
  31. echo "[bar]\n";
  32. $ary["bar"] ??= "foo";
  33. echo "[null]\n";
  34. $ary["null"] ??= "baz";
  35. echo "[foo][bar]\n";
  36. $ary["foo"]["bar"] ??= "abc";
  37. echo "[foo][bar]\n";
  38. $ary["foo"]["bar"] ??= "def";
  39. ?>
  40. --EXPECT--
  41. [foo]
  42. offsetExists(foo)
  43. offsetGet(foo)
  44. [bar]
  45. offsetExists(bar)
  46. offsetSet(bar,foo)
  47. [null]
  48. offsetExists(null)
  49. offsetGet(null)
  50. offsetSet(null,baz)
  51. [foo][bar]
  52. offsetExists(foo)
  53. offsetGet(foo)
  54. offsetExists(bar)
  55. offsetGet(foo)
  56. offsetSet(bar,abc)
  57. [foo][bar]
  58. offsetExists(foo)
  59. offsetGet(foo)
  60. offsetExists(bar)
  61. offsetGet(bar)