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]