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.

190 lines
4.0 KiB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. --TEST--
  2. ZE2 ArrayAccess and ArrayProxyAccess, ArrayProxy
  3. --FILE--
  4. <?php
  5. // NOTE: This will become part of SPL
  6. interface ArrayProxyAccess extends ArrayAccess
  7. {
  8. function proxyGet($element);
  9. function proxySet($element, $index, $value);
  10. function proxyUnset($element, $index);
  11. }
  12. class ArrayProxy implements ArrayAccess
  13. {
  14. private $object;
  15. private $element;
  16. function __construct(ArrayProxyAccess $object, $element)
  17. {
  18. echo __METHOD__ . "($element)\n";
  19. if (!$object->offsetExists($element))
  20. {
  21. $object[$element] = array();
  22. }
  23. $this->object = $object;
  24. $this->element = $element;
  25. }
  26. function offsetExists($index) {
  27. echo __METHOD__ . "($this->element, $index)\n";
  28. return array_key_exists($index, $this->object->proxyGet($this->element));
  29. }
  30. function offsetGet($index) {
  31. echo __METHOD__ . "($this->element, $index)\n";
  32. $tmp = $this->object->proxyGet($this->element);
  33. return isset($tmp[$index]) ? $tmp[$index] : NULL;
  34. }
  35. function offsetSet($index, $value) {
  36. echo __METHOD__ . "($this->element, $index, $value)\n";
  37. $this->object->proxySet($this->element, $index, $value);
  38. }
  39. function offsetUnset($index) {
  40. echo __METHOD__ . "($this->element, $index)\n";
  41. $this->object->proxyUnset($this->element, $index);
  42. }
  43. }
  44. class Peoples implements ArrayProxyAccess
  45. {
  46. public $person;
  47. function __construct()
  48. {
  49. $this->person = array(array('name'=>'Foo'));
  50. }
  51. function offsetExists($index)
  52. {
  53. return array_key_exists($index, $this->person);
  54. }
  55. function offsetGet($index)
  56. {
  57. return new ArrayProxy($this, $index);
  58. }
  59. function offsetSet($index, $value)
  60. {
  61. $this->person[$index] = $value;
  62. }
  63. function offsetUnset($index)
  64. {
  65. unset($this->person[$index]);
  66. }
  67. function proxyGet($element)
  68. {
  69. return $this->person[$element];
  70. }
  71. function proxySet($element, $index, $value)
  72. {
  73. $this->person[$element][$index] = $value;
  74. }
  75. function proxyUnset($element, $index)
  76. {
  77. unset($this->person[$element][$index]);
  78. }
  79. }
  80. $people = new Peoples;
  81. var_dump($people->person[0]['name']);
  82. $people->person[0]['name'] = $people->person[0]['name'] . 'Bar';
  83. var_dump($people->person[0]['name']);
  84. $people->person[0]['name'] .= 'Baz';
  85. var_dump($people->person[0]['name']);
  86. echo "===ArrayOverloading===\n";
  87. $people = new Peoples;
  88. var_dump($people[0]);
  89. var_dump($people[0]['name']);
  90. $people[0]['name'] = 'FooBar';
  91. var_dump($people[0]['name']);
  92. $people[0]['name'] = $people->person[0]['name'] . 'Bar';
  93. var_dump($people[0]['name']);
  94. $people[0]['name'] .= 'Baz';
  95. var_dump($people[0]['name']);
  96. unset($people[0]['name']);
  97. var_dump($people[0]);
  98. var_dump($people[0]['name']);
  99. $people[0]['name'] = 'BlaBla';
  100. var_dump($people[0]['name']);
  101. ?>
  102. ===DONE===
  103. --EXPECTF--
  104. string(3) "Foo"
  105. string(6) "FooBar"
  106. string(9) "FooBarBaz"
  107. ===ArrayOverloading===
  108. ArrayProxy::__construct(0)
  109. object(ArrayProxy)#1 (2) {
  110. ["object:private"]=>
  111. object(Peoples)#2 (1) {
  112. ["person"]=>
  113. array(1) {
  114. [0]=>
  115. array(1) {
  116. ["name"]=>
  117. string(3) "Foo"
  118. }
  119. }
  120. }
  121. ["element:private"]=>
  122. int(0)
  123. }
  124. ArrayProxy::__construct(0)
  125. ArrayProxy::offsetGet(0, name)
  126. string(3) "Foo"
  127. ArrayProxy::__construct(0)
  128. ArrayProxy::offsetSet(0, name, FooBar)
  129. ArrayProxy::__construct(0)
  130. ArrayProxy::offsetGet(0, name)
  131. string(6) "FooBar"
  132. ArrayProxy::__construct(0)
  133. ArrayProxy::offsetSet(0, name, FooBarBar)
  134. ArrayProxy::__construct(0)
  135. ArrayProxy::offsetGet(0, name)
  136. string(9) "FooBarBar"
  137. ArrayProxy::__construct(0)
  138. ArrayProxy::offsetGet(0, name)
  139. ArrayProxy::offsetSet(0, name, FooBarBarBaz)
  140. ArrayProxy::__construct(0)
  141. ArrayProxy::offsetGet(0, name)
  142. string(12) "FooBarBarBaz"
  143. ArrayProxy::__construct(0)
  144. ArrayProxy::offsetUnset(0, name)
  145. ArrayProxy::__construct(0)
  146. object(ArrayProxy)#1 (2) {
  147. ["object:private"]=>
  148. object(Peoples)#2 (1) {
  149. ["person"]=>
  150. array(1) {
  151. [0]=>
  152. array(0) {
  153. }
  154. }
  155. }
  156. ["element:private"]=>
  157. int(0)
  158. }
  159. ArrayProxy::__construct(0)
  160. ArrayProxy::offsetGet(0, name)
  161. NULL
  162. ArrayProxy::__construct(0)
  163. ArrayProxy::offsetSet(0, name, BlaBla)
  164. ArrayProxy::__construct(0)
  165. ArrayProxy::offsetGet(0, name)
  166. string(6) "BlaBla"
  167. ===DONE===