PHP design pattern: iterator pattern (5)

Source: Internet
Author: User
Tags rewind

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.