You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
19 years ago
19 years ago
  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) {
  11. return array_key_exists($this->person, $index);
  12. }
  13. function offsetGet($index) {
  14. return $this->person[$index];
  15. }
  16. function offsetSet($index, $value) {
  17. $this->person[$index] = $value;
  18. }
  19. function offsetUnset($index) {
  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. ===DONE===
  44. --EXPECTF--
  45. string(3) "Joe"
  46. string(6) "JoeFoo"
  47. string(9) "JoeFooBar"
  48. ---ArrayOverloading---
  49. array(1) {
  50. ["name"]=>
  51. string(3) "Joe"
  52. }
  53. string(3) "Joe"
  54. string(6) "JoeFoo"
  55. array(1) {
  56. ["name"]=>
  57. string(6) "JoeFoo"
  58. }
  59. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_005.php on line 46
  60. string(6) "JoeFoo"
  61. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_005.php on line 48
  62. string(6) "JoeFoo"
  63. ===DONE===