array_access_008.phpt 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. --TEST--
  2. ZE2 ArrayAccess and ASSIGN_OP operators (.=)
  3. --FILE--
  4. <?php
  5. class Peoples implements ArrayAccess {
  6. public $person;
  7. function __construct() {
  8. $this->person = array(array('name'=>'Foo'));
  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'] . 'Bar';
  26. var_dump($people->person[0]['name']);
  27. $people->person[0]['name'] .= 'Baz';
  28. var_dump($people->person[0]['name']);
  29. echo "===ArrayOverloading===\n";
  30. $people = new Peoples;
  31. var_dump($people[0]['name']);
  32. $people[0]['name'] = 'FooBar';
  33. var_dump($people[0]['name']);
  34. $people[0]['name'] = $people->person[0]['name'] . 'Bar';
  35. var_dump($people[0]['name']);
  36. $people[0]['name'] .= 'Baz';
  37. var_dump($people[0]['name']);
  38. ?>
  39. --EXPECTF--
  40. string(3) "Foo"
  41. string(6) "FooBar"
  42. string(9) "FooBarBaz"
  43. ===ArrayOverloading===
  44. string(3) "Foo"
  45. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_008.php on line 40
  46. string(3) "Foo"
  47. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_008.php on line 42
  48. string(3) "Foo"
  49. Notice: Indirect modification of overloaded element of Peoples has no effect in %sarray_access_008.php on line 44
  50. string(3) "Foo"