Note that the Iterator is better abstract than the object set, because we can make InfiniteIterators, NoRewindIterators, and so on without having to be consistent with the regular array. Therefore, Iterator lacks functions such as the count () function.
You can find the complete SPL iterator list in the official PHP manual. Thanks to the strong support for PHP, most of the work using the Iterator mode is included in the standard implementation. The following code example uses the functions of the standard Iterator and RecursiveIterators.
___ FCKpd ___ 0 ___ FCKpd ___ 1
// Lets play with RecursiveIterator implementations $ it = new RecursiveArrayIterator (array (A, B, array (C, D), array (E, F), array (G, h, I); // $ it is a RecursiveIterator but also an Iterator, // so it loops normally over the four elements // of the array. echo "Foreach over a RecursiveIterator:"; foreach ($ it as $ value) {echo $ value; // but RecursiveIterators specify additional // Methods to perform e children nodes $ children = $ it-> hasChildren ()? {Yes }:{ No}; echo $ children,;} echo ""; // we can bridge it to a different contract via // a RecursiveIteratorIterator, whose cryptic name // shocould be read as an Iterator that spans over // a RecursiveIterator. echo "Foreach over a RecursiveIteratorIterator:"; foreach (new RecursiveIteratorIterator ($ it) as $ value) {echo $ value;} echo "; Original Name: Practical Php Patterns: iterator Author: Giorgio
Content;
Private $ _ index = 0;
Public function _ construct (array $ content)
{
$ This-> _ content = $ content;
}
Public function rewind ()
{
$ This-> _ index = 0;
}
Public function valid ()
{
Return isset ($ this-> _ content [$ this-> _ index]);
}
Public function current ()
{
Return $ this-> _ content [$ this-> _ index];
}
Public function key ()
{
Return $ this-> _ index;
}
Public function next ()
{
$ This-> _ index ++;
}
}
$ Arrayarray = array (A, B, C, D );
Echo "Collection :";
Foreach (new Collection ($ array) as $ key => $ value ){
Echo "$ key => $ value .";
}
Echo "";
___ FCKpd ___ 1 ___ FCKpd ___ 2 Original Name: Practical Php Patterns: Iterator Author: Giorgio
Content;
Public function _ construct (array $ content)
{
$ This-> _ content = $ content;
}
Public function contains ($ number)
{
Return in_array ($ number, $ this-> _ content );
}
/**
* Only this method is necessary to implement IteratorAggregate.
* @ Return Iterator
*/
Public function getIterator ()
{
Return new ArrayIterator ($ this-> _ content );
}
}
Echo "NumbersSet :";
Foreach (new NumbersSet ($ array) as $ key => $ value ){
Echo "$ key => $ value .";
}
Echo "";
___ FCKpd ___ 2 Original article name: Practical Php Patterns: Iterator Author: Giorgio
Content;
Private $ _ index = 0;
Public function _ construct (array $ content)
{
$ This-> _ content = $ content;
}
Public function rewind ()
{
$ This-> _ index = 0;
}
Public function valid ()
{
Return isset ($ this-> _ content [$ this-> _ index]);
}
Public function current ()
{
Return $ this-> _ content [$ this-> _ index];
}
Public function key ()
{
Return $ this-> _ index;
}
Public function next ()
{
$ This-> _ index ++;
}
}
$ Arrayarray = array (A, B, C, D );
Echo "Collection :";
Foreach (new Collection ($ array) as $ key => $ value ){
Echo "$ key => $ value .";
}
Echo "";
___ FCKpd ___ 1 ___ FCKpd ___ 2