PHP 迭代器模式 一个简单的迭代器

来源:互联网 发布:jpz45加强后数据105炮 编辑:程序博客网 时间:2024/06/11 14:44
<?phpheader("Content-Type:text/html;charset=utf-8");abstract class llIterator{    //  第一个    public abstract function First();    //  最后一个    public abstract function End();    //  下一个    public abstract function Next();    //  前一个    public abstract function Pre();    //  当前    public abstract function Current();    //  指针返回    public abstract function back();}class Con extends llIterator{    private $any;    private $current = 0;    public function __construct(array $anys)    {        $this->any = $anys;    }    public function First()    {        $this->current = 0;        return $this->any[0];    }    public function End()    {        $this->current = count($this->any) - 1;        return $this->any[count($this->any) - 1];    }    public function Next()    {        $this->current++;        return $this->any[$this->current];    }    public function Pre()    {        $this->current--;        return $this->any[$this->current > 5 ? 4 : $this->current];    }    public function Current()    {        return $this->any[$this->current > 5 ? 4 : $this->current];    }    public function back()    {        $this->current = 0;    }}$test = new Con(array('蓝猫','火猫','土猫','大熊猫','龙鹰'));echo $test->First();echo $test->Next();echo $test->End();echo $test->Current();echo $test->Pre();echo $test->back();

原创粉丝点击