can help construct specific objects that can provide a single standard interface loop or iterate over any type of data that can be counted. (not particularly common in PHP)
1. Access the contents of an aggregated object without exposing its internal representation.
2. Support for multiple traversal of aggregation objects.
3. Provides a unified interface for traversing different aggregation structures (that is, polymorphic iterations).
PHP Code implementation:
<?PHP//iterators: Can help construct specific objects that can provide a single standard interface loop or iterate over any type of data that can be counted classMyiteratorImplementsIterator {Private $var=Array(); Public function__construct ($array) { $this-var=$array; } Public function Rewind() { Reset($this-var); } Public function Current() { $var= Current($this-var); return $var; } Public functionvalid () {$var=$this- Current() !==false; return $var; } Public function Next() { $var=Next($this-var); return $var; } Public function Key() { $var=Key($this-var); return $var; } } $values=Array(' A ', ' B ', ' C '); $it=NewMyiterator ($values); foreach($it as $a=$b) { Print"$a:$b<br> "; } ?>
From: http://blog.csdn.net/initphp/article/details/7693576
PHP Design Pattern Series-iterators