language/oop5/iterations.xml
1fb0ef23d7be0d8ecd9604fce16ee1e0842c6ef6
...
...
@@ -4,7 +4,7 @@
4
4
<title>Object Iteration</title>
5
5
<para>
6
6

7
-
PHP 5 provides a way for objects to be defined so it is possible to iterate
7
+
PHP provides a way for objects to be defined so it is possible to iterate
8
8
through a list of items, with, for example a &foreach; statement. By default,
9
9
all <link linkend="language.oop5.visibility">visible</link> properties will be used
10
10
for the iteration.
...
...
@@ -69,191 +69,20 @@ private => private var
69
69
<link linkend="language.oop5.visibility">visible</link> properties that could be
70
70
accessed.
71
71
</para>
72
-
<para>
73
-
To take it a step further, the <interfacename>Iterator</interfacename>
74
-
<link linkend="language.oop5.interfaces">interface</link> may be implemented.
75
-
This allows the object to dictate how it will be iterated and what values will
76
-
be available on each iteration.
77
-
</para>
78
-

79
-
<example>
80
-
<title>Object Iteration implementing Iterator</title>
81
-
<programlisting role="php">
82
-
<![CDATA[
83
-
<?php
84
-
class MyIterator implements Iterator
85
-
{
86
-
private $var = array();
87
-

88
-
public function __construct($array)
89
-
{
90
-
if (is_array($array)) {
91
-
$this->var = $array;
92
-
}
93
-
}
94
-

95
-
public function rewind()
96
-
{
97
-
echo "rewinding\n";
98
-
reset($this->var);
99
-
}
100
-
101
-
public function current()
102
-
{
103
-
$var = current($this->var);
104
-
echo "current: $var\n";
105
-
return $var;
106
-
}
107
-
108
-
public function key()
109
-
{
110
-
$var = key($this->var);
111
-
echo "key: $var\n";
112
-
return $var;
113
-
}
114
-
115
-
public function next()
116
-
{
117
-
$var = next($this->var);
118
-
echo "next: $var\n";
119
-
return $var;
120
-
}
121
-
122
-
public function valid()
123
-
{
124
-
$key = key($this->var);
125
-
$var = ($key !== NULL && $key !== FALSE);
126
-
echo "valid: $var\n";
127
-
return $var;
128
-
}
129
-

130
-
}
131
-

132
-
$values = array(1,2,3);
133
-
$it = new MyIterator($values);
134
-

135
-
foreach ($it as $a => $b) {
136
-
print "$a: $b\n";
137
-
}
138
-
?>
139
-
]]>
140
-
</programlisting>
141
-
&example.outputs;
142
-
<screen role="php">
143
-
<![CDATA[
144
-
rewinding
145
-
valid: 1
146
-
current: 1
147
-
key: 0
148
-
0: 1
149
-
next: 2
150
-
valid: 1
151
-
current: 2
152
-
key: 1
153
-
1: 2
154
-
next: 3
155
-
valid: 1
156
-
current: 3
157
-
key: 2
158
-
2: 3
159
-
next:
160
-
valid:
161
-
]]>
162
-
</screen>
163
-

164
-
</example>
165
72

73
+
<simplesect role="seealso">
74
+
&reftitle.seealso;
166
75
<para>
167
-
The <interfacename>IteratorAggregate</interfacename>
168
-
<link linkend="language.oop5.interfaces">interface</link>
169
-
can be used as an alternative to implementing all of the
170
-
<interfacename>Iterator</interfacename> methods.
171
-
<interfacename>IteratorAggregate</interfacename> only requires the
172
-
implementation of a single method,
173
-
<methodname>IteratorAggregate::getIterator</methodname>, which should return
174
-
an instance of a class implementing <interfacename>Iterator</interfacename>.
76
+
<simplelist>
77
+
<member><link linkend="language.generators">Generators</link></member>
78
+
<member><interfacename>Iterator</interfacename></member>
79
+
<member><interfacename>IteratorAggregate</interfacename> </member>
80
+
<member><link linkend="spl.iterators">SPL Iterators</link></member>
81
+
</simplelist>
175
82
</para>
176
-

177
-
<example>
178
-
<title>Object Iteration implementing IteratorAggregate</title>
179
-
<programlisting role="php">
180
-
<![CDATA[
181
-
<?php
182
-
class MyCollection implements IteratorAggregate
183
-
{
184
-
private $items = array();
185
-
private $count = 0;
186
-

187
-
// Required definition of interface IteratorAggregate
188
-
public function getIterator() {
189
-
return new MyIterator($this->items);
190
-
}
191
-

192
-
public function add($value) {
193
-
$this->items[$this->count++] = $value;
194
-
}
195
-
}
196
-

197
-
$coll = new MyCollection();
198
-
$coll->add('value 1');
199
-
$coll->add('value 2');
200
-
$coll->add('value 3');
201
-

202
-
foreach ($coll as $key => $val) {
203
-
echo "key/value: [$key -> $val]\n\n";
204
-
}
205
-
?>
206
-
]]>
207
-
</programlisting>
208
-
&example.outputs;
209
-
<screen role="php">
210
-
<![CDATA[
211
-
rewinding
212
-
current: value 1
213
-
valid: 1
214
-
current: value 1
215
-
key: 0
216
-
key/value: [0 -> value 1]
217
-

218
-
next: value 2
219
-
current: value 2
220
-
valid: 1
221
-
current: value 2
222
-
key: 1
223
-
key/value: [1 -> value 2]
224
-

225
-
next: value 3
226
-
current: value 3
227
-
valid: 1
228
-
current: value 3
229
-
key: 2
230
-
key/value: [2 -> value 3]
231
-

232
-
next:
233
-
current:
234
-
valid:
235
-
]]>
236
-
</screen>
237
-

238
-
</example>
239
-

240
-
<note>
241
-
<para>
242
-
For more examples of iterators, see the
243
-
<link linkend="spl.iterators">SPL Extension</link>.
244
-
</para>
245
-
</note>
246
-

247
-
<note>
248
-
<para>
249
-
Users of PHP 5.5 and later may also want to investigate
250
-
<link linkend="language.generators">generators</link>, which provide an
251
-
alternative way of defining iterators.
252
-
</para>
253
-
</note>
83
+
</simplesect>
254
84

255
85
</sect1>
256
-
257
86
<!-- Keep this comment at the end of the file
258
87
Local variables:
259
88
mode: sgml
260
89