array_access_003.phpt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --TEST--
  2. ZE2 ArrayAccess::offsetGet ambiguities
  3. --INI--
  4. error_reporting=4095
  5. --FILE--
  6. <?php
  7. class ObjectOne implements ArrayAccess {
  8. public $a = array('1st', 1, 2=>'3rd', '4th'=>4);
  9. function offsetExists($index): bool {
  10. echo __METHOD__ . "($index)\n";
  11. return array_key_exists($index, $this->a);
  12. }
  13. function offsetGet($index): mixed {
  14. echo __METHOD__ . "($index)\n";
  15. switch($index) {
  16. case 1:
  17. $a = 'foo';
  18. return $a . 'Bar';
  19. case 2:
  20. static $a=1;
  21. return $a;
  22. }
  23. return $this->a[$index];
  24. }
  25. function offsetSet($index, $newval): void {
  26. echo __METHOD__ . "($index,$newval)\n";
  27. if ($index==3) {
  28. $this->cnt = $newval;
  29. }
  30. $this->a[$index] = $newval;
  31. }
  32. function offsetUnset($index): void {
  33. echo __METHOD__ . "($index)\n";
  34. unset($this->a[$index]);
  35. }
  36. }
  37. $obj = new ObjectOne;
  38. var_dump($obj[1]);
  39. var_dump($obj[2]);
  40. $obj[2]++;
  41. var_dump($obj[2]);
  42. ?>
  43. --EXPECTF--
  44. ObjectOne::offsetGet(1)
  45. string(6) "fooBar"
  46. ObjectOne::offsetGet(2)
  47. int(1)
  48. ObjectOne::offsetGet(2)
  49. Notice: Indirect modification of overloaded element of ObjectOne has no effect in %sarray_access_003.php on line 39
  50. ObjectOne::offsetGet(2)
  51. int(1)