Copy Code code 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 Run output:
Copy Code code as follows:
String (Myiterator::rewind)
String (myiterator::valid)
String (myiterator::current)
String (Myiterator::key)
Output key value: Int (0)
String (first_element)
String (Myiterator::next)
String (myiterator::valid)
String (myiterator::current)
String (Myiterator::key)
Output key value: Int (1)
String "Second_element"
String (Myiterator::next)
String (myiterator::valid)
String (myiterator::current)
String (Myiterator::key)
Output key value: Int (2)
String (a) "Last_element"
String (Myiterator::next)
String (myiterator::valid)
The following methods are required within a general iterator:
Iterator::current-return the current element returns the
Iterator::key-return the key of the current element returns the keys
Iterator::next-move forward to next element moves to the next
Iterator::rewind-rewind the iterator to the first element again
Iterator::valid-checks If position is valid check the validity of the current position
If you are not very clear about the content workflow of the iterator, you can view the traversal of the following iterator arrays:
Copy Code code 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 "Rewind 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 "The 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 "At this time the key value pair-key $k: value $v \ n";
}
Program Run Result:
Copy Code code as follows:
Rewind back to the first element
Current element: 1
Check Validity: 1
Current element: 1
Keys for current element: 0
At this point the key value pair--key 0:value 1
Move to next element: 2
Current element: 2
Check Validity: 1
Current element: 2
Keys for current element: 1
At this point the key value pair--key 1:value 2
Move to next element: 3
Current element: 3
Check Validity: 1
Current element: 3
Keys for current element: 2
At this point the key value pair--key 2:value 3
Move to the next element:
Current element:
Check Validity:
Is it clear now?