A detailed _php example of the internal execution process of the PHP iterator

Source: Internet
Author: User
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 Run output:
Copy the 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 ("Last_element")

String (+) "Myiterator::next"
String ("Myiterator::valid")

The following method is required inside a generic iterator:
Iterator::current-return the current element returns
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 location
If the content workflow of the iterator is not clear, you can view the following iterator's traversal process for the array:
Copy the 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 "Key of 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 (+/-);
$it = new Myiterator ($values);

foreach ($it as $k = = $v) {
Print "At this time key value pair-key $k: value $v \ n \ nthe";
}

Program Run Result:
Copy the Code code as follows:
Rewind the first element
Current element: 1
Check Validity: 1
Current element: 1
Key for current element: 0
At this point the key value pair--key 0:value 1

Move to the next element: 2
Current element: 2
Check Validity: 1
Current element: 2
Key for current element: 1
At this point the key value pair--key 1:value 2

Move to the next element: 3
Current element: 3
Check Validity: 1
Current element: 3
Key for current element: 2
At this point the key value pair--key 2:value 3

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

It's clear now, isn't it?

  • 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.