Iterator while迭代

最后更新于:2022-04-02 02:26:21

[TOC] ## 实例 ``` class demo implements Iterator { public $a=[1,2,3,4]; public $index=0; public function current(){ return $this->index; } public function next(){ $this->index++; return $this->a[$this->index]; } public function key(){ return $this->index; } public function valid(){ return isset($this->a[$this->index]); } public function rewind(){ return $this->index=0; } } $demo =new demo(); // 使用 foreach //foreach ($demo as $k=>$v){ // var_dump($v); //} // 使用 while while($demo->valid()){ var_dump($demo->current()); $demo->next(); } ```
';