Explanation of the internal execution process of the PHP iterator

Source: Internet
Author: User
Tags rewind

Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
/**
* @ Author concise modern magic http://www.nowamagic.net
*/
Class MyIterator implements Iterator
{
Private $ var = array ();

Public function _ construct ($ array)
{
If (is_array ($ array )){
$ This-> var = $ array;
}
}

Public function rewind (){
Echo "returns the first element \ n ";
Reset ($ this-> var );
}

Public function current (){
$ Var = current ($ this-> var );
Echo "Current element: $ var \ n ";
Return $ var;
}

Public function key (){
$ Var = key ($ this-> var );
Echo "Key of the current element: $ var \ n ";
Return $ var;
}

Public function next (){
$ Var = next ($ this-> var );
Echo "Move to the next element: $ var \ n ";
Return $ var;
}

Public function valid (){
$ Var = $ this-> current ()! = False;
Echo "check validity: {$ var} \ n ";
Return $ var;
}
}

$ Values = array (1, 2, 3 );
$ It = new MyIterator ($ values );

Foreach ($ it as $ k => $ v ){
Print "key-value pair -- key $ k: value $ v \ n ";
}

Program running result:
Copy codeThe Code is as follows:
Returns the first element.
Current element: 1
Check validity: 1
Current element: 1
Key of the current element: 0
Key-value pair -- key 0: value 1

Move to the next element: 2
Current element: 2
Check validity: 1
Current element: 2
Key of the current element: 1
Key-value pair -- key 1: value 2

Move to the next element: 3
Current element: 3
Check validity: 1
Current element: 3
Key of the current element: 2
Key-value pair -- key 2: value 3

Move to the next element:
Current element:
Check validity:

Is it clear now?

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.