Check the internal execution process of the PHP iterator.

Source: Internet
Author: User
Tags rewind
Welcome to the Linux community forum and interact with 2 million technical staff. Next we will learn how to implement a custom iterator, and then begin to understand the internal working principles of the iterator. Let's take a look at an official example :? PhpclassmyIteratorimplementsIterator {private $ position0; private $ arra

Welcome to the Linux community forum and interact with 2 million technical staff> next we will learn how to implement a custom iterator, and then start to understand the internal working principles of the iterator. Let's take a look at an official example :? Php class myIterator implements Iterator {private $ position = 0; private $ arra

Welcome to the Linux community forum and interact with 2 million technicians>

Next we will learn how to implement a custom iterator, and then start to understand the internal working principle of the iterator. Let's take a look at an official example:

  

Class myIterator implements Iterator {

Private $ position = 0;

Private $ array = array (

"First_element ",

"Second_element ",

"Last_element ",

);

Public function _ construct (){

$ This-> position = 0;

}

Function rewind (){

Var_dump (_ METHOD __);

$ This-> position = 0;

}

Function current (){

Var_dump (_ METHOD __);

Return $ this-> array [$ this-> position];

}

Function key (){

Var_dump (_ METHOD __);

Return $ this-> position;

}

Function next (){

Var_dump (_ METHOD __);

++ $ This-> position;

}

Function valid (){

Var_dump (_ METHOD __);

Return isset ($ this-> array [$ this-> position]);

}

}

$ It = new myIterator;

Foreach ($ it as $ key => $ value ){

Echo 'output key value :';

Var_dump ($ key, $ value );

// Echo $ key;

Echo "\ n ";

}

Program running output:

String (18) "myIterator: rewind"

String (17) "myIterator: valid"

String (19) "myIterator: current"

String (15) "myIterator: key"

Output key value: int (0)

String (13) "first_element"

String (16) "myIterator: next"

String (17) "myIterator: valid"

String (19) "myIterator: current"

String (15) "myIterator: key"

Output key value: int (1)

String (14) "second_element"

String (16) "myIterator: next"

String (17) "myIterator: valid"

String (19) "myIterator: current"

String (15) "myIterator: key"

Output key value: int (2)

String (12) "last_element"

String (16) "myIterator: next"

String (17) "myIterator: valid"

The following method is required for an iterator:

Iterator: current-Return the current element returns the current element

Iterator: key-Return the key of the current element returns the key of the current element

Iterator: next-Move forward to next element Move to the next element

Iterator: rewind-Rewind the Iterator to the first element to return to the first element

Iterator: valid-Checks if current position is valid check validity of the current position

If you are not clear about the content workflow of the iterator, you can view the traversal process of the array by the iterator below:

  

/**

* @ Author nicesunboy@gmail.com

*/

Class MyIterator implements Iterator

{

Private $ var = array ();

Public function _ construct ($ array)

{

If (is_array ($ array )){

$ This-> var = $ array;

}

}

[1] [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.