Home  >  Article  >  Backend Development  >  Take a look at the internal execution process of PHP iterator_PHP tutorial

Take a look at the internal execution process of PHP iterator_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:16744browse

Let’s learn how to implement a custom iterator, and then slowly start to understand the inner workings of iterators. Let’s take a look at an official example first:

<?php
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 '输出键值:';
    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"
输出键值:int(0)
string(13) "first_element"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
输出键值:int(1)
string(14) "second_element"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
输出键值:int(2)
string(12) "last_element"

string(16) "myIterator::next"
string(17) "myIterator::valid"

General iterators require the following methods internally:

  • Iterator::current — Return the current element Return the current element
  • Iterator::key — Return the key of the current element Return the key of the current element
  • Iterator::next — Move forward to next element Move to Next element
  • Iterator::rewind — Rewind the Iterator to the first element Return to the first element
  • Iterator::valid — Checks if current 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 following iterator traversal process of the array:

    <?php
    /**
    * @author nicesunboy@gmail.com
    */
    class MyIterator implements Iterator
    {
         private $var = array();
    
         public function __construct($array)
         {
             if (is_array($array)) {
                $this->var = $array;
             }
         }
    
         public function rewind() {
             echo "倒回第一个元素\n";
            reset($this->var);
         }
    
         public function current() {
            $var = current($this->var);
             echo "当前元素: $var\n";
             return $var;
         }
    
         public function key() {
            $var = key($this->var);
             echo "当前元素的键: $var\n";
             return $var;
         }
    
         public function next() {
            $var = next($this->var);
             echo "移向下一个元素: $var\n";
             return $var;
         }
    
         public function valid() {
            $var = $this->current() !== false;
             echo "检查有效性: {$var}\n";
             return $var;
         }
    }
    
    $values = array(1,2,3);
    $it = new MyIterator($values);
    
    foreach ($it as $k => $v) {
         print "此时键值对 -- key $k: value $v\n\n";
    }

    Program running result:

    倒回第一个元素
    当前元素: 1
    检查有效性: 1
    当前元素: 1
    当前元素的键: 0
    此时键值对 -- key 0: value 1
    
    移向下一个元素: 2
    当前元素: 2
    检查有效性: 1
    当前元素: 2
    当前元素的键: 1
    此时键值对 -- key 1: value 2
    
    移向下一个元素: 3
    当前元素: 3
    检查有效性: 1
    当前元素: 3
    当前元素的键: 2
    此时键值对 -- key 2: value 3
    
    移向下一个元素: 
    当前元素: 
    检查有效性: 

    Is it clear now?



    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/777573.htmlTechArticleLet’s learn how to implement a custom iterator, and then slowly start to understand the inner workings of the iterator principle. Let’s look at an official example first: position = 0; } functio...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn