user2243528
user2243528

Reputation: 435

some questions about the usage of Iterator function

Code is from:http://php.net/manual/en/class.iterator.php(Example #1 Basic usage)

<?php
class myIterator implements Iterator {
    private $position = 0;
    private $array = array(
        "firstelement",
        "secondelement",
        "lastelement",
    );  

    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) {
    var_dump($key, $value);
    echo "\n";
}
?>

Output is:

string 'myIterator::rewind' (length=18)

string 'myIterator::valid' (length=17)

string 'myIterator::current' (length=19)

string 'myIterator::key' (length=15)

int 0

string 'firstelement' (length=12)

string 'myIterator::next' (length=16)

string 'myIterator::valid' (length=17)

string 'myIterator::current' (length=19)

string 'myIterator::key' (length=15)

int 1

string 'secondelement' (length=13)

string 'myIterator::next' (length=16)

string 'myIterator::valid' (length=17)

string 'myIterator::current' (length=19)

string 'myIterator::key' (length=15)

int 2

string 'lastelement' (length=11)

string 'myIterator::next' (length=16)

string 'myIterator::valid' (length=17)

So we can see: 1st, the order of function(var_dump(__METHOD__)) output is:

string 'myIterator::rewind' (length=18)
string 'myIterator::valid' (length=17)
string 'myIterator::current' (length=19)
string 'myIterator::key' (length=15)

2nd and 3rd, the order of function output is: the order of function output is:

string 'myIterator::next' (length=16)
string 'myIterator::valid' (length=17)
string 'myIterator::current' (length=19)
string 'myIterator::key' (length=15)

4rd, the order of function output is:

string 'myIterator::next' (length=16)
string 'myIterator::valid' (length=17)

My question is:

  1. there are 5 functions: rewind();current(); key(); next(); valid(); why some functions seems not execute sometimes? eg, we can not see myIterator::next in the 1st function output. and also rewind() only shows one time.
  2. there are 3 values in $array, so supposed foreach only run 3 times, but we can see string 'myIterator::valid' (length=17) appear 4 times, why?

Upvotes: 0

Views: 52

Answers (1)

huysentruitw
huysentruitw

Reputation: 28091

The first part is the initialization of the for-loop. It calls rewind to move the cursor to the beginning of the list. That's also why you don't see a next there.

The last part does a next but then, the for-loop realizes that the end of list is reached, because valid returned false. So valid is called 4 times, 3 times returning true, then returning false once. How would the for-loop know where to stop otherwise?

Upvotes: 1

Related Questions