Home > Backend Development > PHP Tutorial > Detailed explanation of the internal execution process of PHP iterator_PHP tutorial

Detailed explanation of the internal execution process of PHP iterator_PHP tutorial

WBOY
Release: 2016-07-13 10:25:18
Original
942 people have browsed it

复制代码 代码如下:

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";
}


程序运行输出:
复制代码 代码如下:

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"


一般的迭代器内部需要下面的方法:
Iterator::current — Return the current element 返回当前元素
Iterator::key — Return the key of the current element 返回当前元素的键
Iterator::next — Move forward to next element 移向下一个元素
Iterator::rewind — Rewind the Iterator to the first element 重新回到第一个元素
Iterator::valid — Checks if current position is valid 检查当前位置的有效性
如果不是很清楚迭代器的内容工作流程,可以查看下面的迭代器对数组的遍历过程:
复制代码 代码如下:

/**
* @author Simple Modern Magic http://www.nowamagic.net
*/
class MyIterator implements Iterator
{
private $var = array();

public function __construct($array)
{
if (is_array($array)) {
var = $array;
}
}

public function rewind() {
echo "Rewind the first element n";
reset($this->var);
}

public function current() {
$var = current($this->var);
echo "Current element: $varn";
return $var;
}

public function key() {
$var = key($this->var);
echo "The key of the current element: $varn";
return $var;
}

public function next() {
$var = next($this->var);
echo "Move to next element: $varn";
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 >Program running result:


Copy code
The code is as follows:Rewind the first elementCurrent element: 1
Check validity: 1
Current element: 1
Key of current element: 0
Key-value pair at this time--key 0: value 1

Move to next element: 2
Current element: 2
Check validity: 1Current element: 2

Key of current element: 1
Key-value pair at this time - - key 1: value 2

Move to next element: 3
Current element: 3
Check validity: 1

Current element: 3

Key of current element: 2
Key-value pair at this time - - key 2: value 3

Move to next element:
Current element:
Check validity:


That’s clear now, right?


http://www.bkjia.com/PHPjc/825138.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/825138.htmlTechArticleCopy the code code as follows: class myIterator implements Iterator { private $position = 0; private $array = array( "first_element ", "second_element", "last_element", ); public functi...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template