Fixed for bug #30588, changed wording and provide an example.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@171425 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Curt Zirzow 2004-10-28 02:50:22 +00:00
parent ec326172db
commit ce2e38e434

View file

@ -1,12 +1,15 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.8 $ -->
<!-- $Revision: 1.9 $ -->
<sect1 id="language.oop5.iterations">
<title>Object Iteration</title>
<para>
PHP 5 provides a way for objects to be defined so it is possible
to iterate through a list of items, with, for example a <link
linkend="control-structures.foreach">foreach</link> statement. By default, all
public properties will be used for the iteration.
PHP 5 provides a way for objects to be defined so it is possible to iterate
through a list of items, with, for example a <link
linkend="control-structures.foreach">foreach</link> statement. By default,
all <link linkend="oop5.visibility">visible</link> properties will be used
for the iteration.
</para>
<example>
@ -20,8 +23,15 @@ class MyClass
public $var2 = 'value 2';
public $var3 = 'value 3';
protected $protected = 'protected';
private $private = 'private';
protected $protected = 'protected var';
private $private = 'private var';
function iterateVisible() {
echo "MyClass::iterateVisible:\n";
foreach($this as $key => $value) {
print "$key => $value\n";
}
}
}
$class = new MyClass();
@ -29,6 +39,11 @@ $class = new MyClass();
foreach($class as $key => $value) {
print "$key => $value\n";
}
echo "\n"
$class->iterateVisible();
?>
]]>
</programlisting>
@ -38,18 +53,27 @@ foreach($class as $key => $value) {
var1 => value 1
var2 => value 2
var3 => value 3
MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var
]]>
</screen>
</example>
<para>
As the output shows, the <link linkend="control-structures.foreach">foreach</link>
iterated through each public variable that is defined. To take it
a step further you can <varname>implement</varname> 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 <link
linkend="control-structures.foreach">foreach</link> iterated through all
<link linkend="oop5.visibility">visible</link> variables that can be
accessed. To take it a step further you can <varname>implement</varname> 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.
</para>
<example>