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