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";
}
程序运行输出:
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 简明现代魔法 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 "倒回第一个元素\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";
}
程序运行结果:
倒回第一个元素
当前元素: 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
移向下一个元素:
当前元素:
检查有效性:
现在就很清晰了吧?