Detail the internal execution process of the PHP iterator

  • 2020-11-18 06:08:52
  • OfStack


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 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 methods are required internally for a 1-like 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 down 1 element
Iterator::rewind -- Rewind the Iterator to the first element returns to the first element
Iterator::valid -- Checks if current position is valid Checks the validity of the current position
If you don't have a clear idea of the iterator's content workflow, you can see the following iterator traversal of an array:

/**
* @author  Brief 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 " Fall back to the first 1 An element \n";
        reset($this->var);
     }
     public function current() {
        $var = current($this->var);
         echo " The 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 " Move down 1 An 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 " Here's the key-value pair  -- key $k: value $v\n\n";
}

Program operation Results:

 Fall back to the first 1 An element 
 The current element : 1
 Check validity : 1
 The current element : 1
 The key of the current element : 0
 Here's the key-value pair  -- key 0: value 1
 Move down 1 An element : 2
 The current element : 2
 Check validity : 1
 The current element : 2
 The key of the current element : 1
 Here's the key-value pair  -- key 1: value 2
 Move down 1 An element : 3
 The current element : 3
 Check validity : 1
 The current element : 3
 The key of the current element : 2
 Here's the key-value pair  -- key 2: value 3
 Move down 1 An element : 
 The current element : 
 Check validity : 

Is that clear now?


Related articles: