PHP Design Pattern Series-iterators

Source: Internet
Author: User

    • 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. (not particularly common in PHP)
    • Usage scenarios:

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

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.