array_access_004.phpt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --TEST--
  2. ZE2 ArrayAccess::offsetGet ambiguties
  3. --FILE--
  4. <?php
  5. class object implements ArrayAccess {
  6. public $a = array('1st', 1, 2=>'3rd', '4th'=>4);
  7. function offsetExists($index) {
  8. echo __METHOD__ . "($index)\n";
  9. return array_key_exists($index, $this->a);
  10. }
  11. function offsetGet($index) {
  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) {
  24. echo __METHOD__ . "($index,$newval)\n";
  25. if ($index==3) {
  26. $this->cnt = $newval;
  27. }
  28. return $this->a[$index] = $newval;
  29. }
  30. function offsetUnset($index) {
  31. echo __METHOD__ . "($index)\n";
  32. unset($this->a[$index]);
  33. }
  34. }
  35. $obj = new Object;
  36. var_dump($obj[1]);
  37. var_dump($obj[2]);
  38. $obj[2]++;
  39. var_dump($obj[2]);
  40. ?>
  41. ===DONE===
  42. --EXPECTF--
  43. object::offsetGet(1)
  44. string(6) "fooBar"
  45. object::offsetGet(2)
  46. int(1)
  47. object::offsetGet(2)
  48. Notice: Indirect modification of overloaded element of object has no effect in %sarray_access_004.php on line 39
  49. object::offsetGet(2)
  50. int(1)
  51. ===DONE===