array_access_004.phpt 1.2 KB

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