array_access_007.phpt 929 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. --TEST--
  2. ZE2 ArrayAccess and [] assignment
  3. --FILE--
  4. <?php
  5. class OverloadedArray implements ArrayAccess {
  6. public $realArray;
  7. function __construct() {
  8. $this->realArray = array();
  9. }
  10. function offsetExists($index): bool {
  11. return array_key_exists($this->realArray, $index);
  12. }
  13. function offsetGet($index): mixed {
  14. return $this->realArray[$index];
  15. }
  16. function offsetSet($index, $value): void {
  17. if (is_null($index)) {
  18. $this->realArray[] = $value;
  19. } else {
  20. $this->realArray[$index] = $value;
  21. }
  22. }
  23. function offsetUnset($index): void {
  24. unset($this->realArray[$index]);
  25. }
  26. function dump() {
  27. var_dump($this->realArray);
  28. }
  29. }
  30. $a = new OverloadedArray;
  31. $a[] = 1;
  32. $a[1] = 2;
  33. $a[2] = 3;
  34. $a[] = 4;
  35. $a->dump();
  36. ?>
  37. --EXPECT--
  38. array(4) {
  39. [0]=>
  40. int(1)
  41. [1]=>
  42. int(2)
  43. [2]=>
  44. int(3)
  45. [3]=>
  46. int(4)
  47. }