Iterator 接口 一共有5个方法 rewind ,key ,current,valid,next
生成器的迭代器的区别 生成器只能迭代一次,而迭代器可以迭代很多次
一个迭代器的例子
class LineIterator implements Iterator{
private $pointer=0;
private $line=false;
private $fileHander=null;
public function __construct($fileName){
$this->fileHander=@fopen($fileName,'r');
}
public function key(){
return $this->pointer;
}
public function rewind(){
fseek($this->fileHander,0);
if(false!==$this->$line=fgets($this->fileHander))
{
$this->pointer=0;
}
}
public function current(){
return $this->line;
}
public function valid(){
return $this->line===false;
}
public function next(){
if(false!==$this->line=fgets($this->fileHander)){
$this->pointer++;
}
}
public function __deconstruct(){
@fclose($this->$fileHander);
}
}