ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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(); } ```