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