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