mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
rework iterator/iteratoraggregate parts of oop5.iteration (no more "you"!)
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@324593 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
848cf45fc3
commit
f130f1800e
1 changed files with 17 additions and 10 deletions
|
@ -65,13 +65,15 @@ private => private var
|
|||
</example>
|
||||
|
||||
<para>
|
||||
As the output shows, the &foreach; iterated through all
|
||||
<link linkend="language.oop5.visibility">visible</link> variables that can be
|
||||
accessed. To take it a step further you can implement one
|
||||
of PHP 5's internal <link
|
||||
linkend="language.oop5.interfaces">interface</link> named
|
||||
<varname>Iterator</varname>. This allows the object to decide what and how
|
||||
the object will be iterated.
|
||||
As the output shows, the &foreach; iterated through all of the
|
||||
<link linkend="language.oop5.visibility">visible</link> properties that could be
|
||||
accessed.
|
||||
</para>
|
||||
<para>
|
||||
To take it a step further, the <interfacename>Iterator</interfacename>
|
||||
<link linkend="language.oop5.interfaces">interface</link> may be implemented.
|
||||
This allows the object to dictate how it will be iterated and what values will
|
||||
be available on each iteration.
|
||||
</para>
|
||||
|
||||
<example>
|
||||
|
@ -162,9 +164,14 @@ valid:
|
|||
</example>
|
||||
|
||||
<para>
|
||||
You can also define your class so that it doesn't have to define
|
||||
all the <varname>Iterator</varname> functions by simply implementing
|
||||
the PHP 5 <varname>IteratorAggregate</varname> interface.
|
||||
The <interfacename>IteratorAggregate</interfacename>
|
||||
<link linkend="language.oop5.interfaces">interface</link>
|
||||
can be used as an alternative to implementing all of the
|
||||
<interfacename>Iterator</interfacename> methods.
|
||||
<interfacename>IteratorAggregate</interfacename> only requires the
|
||||
implementation of a single method,
|
||||
<methodname>IteratorAggregate::getIterator</methodname>, which should return
|
||||
an instance of a class implementing <interfacename>Iterator</interfacename>.
|
||||
</para>
|
||||
|
||||
<example>
|
||||
|
|
Loading…
Reference in a new issue