Let's take a look at the internal execution process of the PHP iterator. next we will learn how to implement a custom iterator, and then begin to understand the internal working principle of the iterator. Let's take a look at an official example: & lt ;? PhpclassmyIteratorimplementsIterator {private $ position0; private $ arrayarray (& quo look at the internal execution process of the PHP iterator
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:
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:
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 "moves 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:
Returns the current element of the first element: 1 check validity: 1 key of the current element: 1 key of the current element: 0 at this time, the key-value pair -- key 0: value 1 moves to the next element: 2 current element: 2 Check validity: 1 current element: 2 key of the current element: 1 at this time, the key-value pair -- key 1: value 2 moves to the next element: 3 current element: 3 Check validity: 1 Current element: 3 key of the current element: 2 at this time, the key-value pair -- key 2: value 3 moves to the next element: current element: Check validity:
Is it clear now?