array_access_005.phpt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --TEST--
  2. ZE2 ArrayAccess and sub Arrays
  3. --FILE--
  4. <?php
  5. class Peoples implements ArrayAccess {
  6. public $person;
  7. function __construct() {
  8. $this->person = array(array('name'=>'Joe'));
  9. }
  10. function offsetExists($index): bool {
  11. return array_key_exists($this->person, $index);
  12. }
  13. function offsetGet($index): mixed {
  14. return $this->person[$index];
  15. }
  16. function offsetSet($index, $value): void {
  17. $this->person[$index] = $value;
  18. }
  19. function offsetUnset($index): void {
  20. unset($this->person[$index]);
  21. }
  22. }
  23. $people = new Peoples;
  24. var_dump($people->person[0]['name']);
  25. $people->person[0]['name'] = $people->person[0]['name'] . 'Foo';
  26. var_dump($people->person[0]['name']);
  27. $people->person[0]['name'] .= 'Bar';
  28. var_dump($people->person[0]['name']);
  29. echo "---ArrayOverloading---\n";
  30. $people = new Peoples;
  31. var_dump($people[0]);
  32. var_dump($people[0]['name']);
  33. var_dump($people->person[0]['name'] . 'Foo'); // impossible to assign this since we don't return references here
  34. $x = $people[0]; // creates a copy
  35. $x['name'] .= 'Foo';
  36. $people[0] = $x;
  37. var_dump($people[0]);
  38. $people[0]['name'] = 'JoeFoo';
  39. var_dump($people[0]['name']);
  40. $people[0]['name'] = 'JoeFooBar';
  41. var_dump($people[0]['name']);
  42. ?>
  43. --EXPECTF--
  44. string(3) "Joe"
  45. string(6) "JoeFoo"
  46. string(9) "JoeFooBar"
  47. ---ArrayOverloading---
  48. array(1) {
  49. ["name"]=>
  50. string(3) "Joe"
  51. }
  52. string(3) "Joe"
  53. string(6) "JoeFoo"
  54. array(1) {
  55. ["name"]=>
  56. string(6) "JoeFoo"
  57. }
  58. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_005.php on line 46
  59. string(6) "JoeFoo"
  60. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_005.php on line 48
  61. string(6) "JoeFoo"