From ce2e38e4344a19f195701f8910eea236e4175c6a Mon Sep 17 00:00:00 2001 From: Curt Zirzow Date: Thu, 28 Oct 2004 02:50:22 +0000 Subject: [PATCH] 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 --- language/oop5/iterations.xml | 50 ++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/language/oop5/iterations.xml b/language/oop5/iterations.xml index 236c5a9a5e..3965ff8c68 100644 --- a/language/oop5/iterations.xml +++ b/language/oop5/iterations.xml @@ -1,12 +1,15 @@ - + Object 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 foreach 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 foreach statement. By default, + all visible properties will be used + for the iteration. + @@ -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(); + ?> ]]> @@ -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 ]]> - As the output shows, the foreach - iterated through each public variable that is defined. To take it - a step further you can implement one of PHP 5's - internal interface named - Iterator. This allows the object to decide what - and how the object will be iterated. + As the output shows, the foreach iterated through all + visible variables that can be + accessed. To take it a step further you can implement one + of PHP 5's internal interface named + Iterator. This allows the object to decide what and how + the object will be iterated.