PHP Iterator Iterator interface

Source: Internet
Author: User

Introduced

An interface for an external iterator or class that can iterate internally.

Iterator extends traversable{/* method */Abstract public mixed current (void) abstract public scalar key (void) AB Stract public void Next (void), abstract public void rewind (void), abstract public boolean valid (void)}


To give a chestnut, define an iterator:

class myierator implements iterator{    protected  $position;//Hands      public  $arr  = [];    public function __ Construct (array  $arr)     {         $info   = date (' y-m-d h:i:s ')  .  '   '  . __METHOD__ .  '  line : '  . __LINE__ .  "\ r \ n";         $this->writelog ($ info);         $this->arr =  $arr;         return  $this;     }    //gets the current value      public function current ()     {          $info  = date (' y-m-d h:i:s ')  .  '   '  . __METHOD__  .  '  line : ' &nbsP;.  __LINE__ .  "\ r \ n";         $this->writelog ($info);         return  $this->arr[$this->position] ??  null;    }    //move the pointer to the next     public  Function next ()     {         $info  =  date (' y-m-d h:i:s ')  .  '   '  . __METHOD__ .  '  line : '  . __LINE__ .  "\ r \ n";         $this->writelog ($ info);         return ++ $this->position;     }    //returns the current pointer     public function key ()      {         $info  = date (' y-m-d h:i:s ')  .   '   '  . __METHOD__ .   '  line : '  . __LINE__ .  ' \ r \ n ';          $this->writelog ($info);        return  $this->position ;     }    //Reset Pointer     public function  Rewind ()     {         $info  = date (' y-m-d  h:i:s ')  .  '   '  . __METHOD__ .  '  line : '  . __line__  .  "\ r \ n";         $this->writelog ($info);          $this->position = 0;    }     //Check whether it is currently active     public function valid ()     {          $info  = date (' y-m-d h:i:s ')  .  '   '  . __method__&nBSP;.   '  line : '  . __LINE__ .  ' \ r \ n ';          $this->writelog ($info);         return isset ($this, arr[$this->position]);     }    private function writelog ( string  $info)     {        error_log ($info,  3,  './debug.log ');     }}


$zhangsan =[' One ', ' one ', ' one ', ' three ', ' four ']; $arr = new Myierator ($zhangsan); foreach ($arr as $k + $v) {echo "{$k} = {$v} \ r \ n ";}

Printing results:

0 = One
1 =
2 = Three
3 = Four

To view the call log:

2017-11-27 15:56:04 myierator::__construct line:16


2017-11-27 15:56:04 myierator::rewind line:50 Reset pointer
2017-11-27 15:56:04 myierator::valid line:58 Check is valid
2017-11-27 15:56:04 myierator::current line:25 Gets the value of the current pointer
2017-11-27 15:56:04 myierator::key line:41 get current pointer position
2017-11-27 15:56:04 myierator::next line:33 pointer moves down one (into the next loop)


2017-11-27 15:56:04 Myierator::valid line:58
2017-11-27 15:56:04 myierator::current line:25
2017-11-27 15:56:04 Myierator::key line:41
2017-11-27 15:56:04 Myierator::next line:33


2017-11-27 15:56:04 Myierator::valid line:58
2017-11-27 15:56:04 myierator::current line:25
2017-11-27 15:56:04 Myierator::key line:41
2017-11-27 15:56:04 Myierator::next line:33


2017-11-27 15:56:04 Myierator::valid line:58
2017-11-27 15:56:04 myierator::current line:25
2017-11-27 15:56:04 Myierator::key line:41
2017-11-27 15:56:04 Myierator::next line:33


2017-11-27 15:56:04 Myierator::valid line:58




//Traverse mode two:
$arr->rewind ();
while($arr->valid ()) {
   $key= $arr->key ();
   $value= $arr->current ();

   Echo"{$key}={$value} \ r \ n";

   $arr->next ();
}


PHP Iterator Iterator interface

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.