search
Homephp教程php手册PHP设计模式 迭代器模式 - tianxintian22

迭代器模式,在不需要了解内部实现的前提下,遍历一个聚合对象的内部元素。相比于传统的编程模式,迭代器模式可以隐藏遍历元素所需要的操作。

AllHacl.php

<span style="color: #000000;">php

namespace Baobab;


</span><span style="color: #0000ff;">class</span> AllHacl <span style="color: #0000ff;">implements</span><span style="color: #000000;"> \iterator{

    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$ids</span><span style="color: #000000;">;</span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$index</span>;<span style="color: #008000;">//</span><span style="color: #008000;">当前位置</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct(){
        </span><span style="color: #800080;">$db</span> = Factory::getDatabase('ha_cl'<span style="color: #000000;">);
        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$db</span>->query('select ID from ha_cl'<span style="color: #000000;">);
        </span><span style="color: #800080;">$this</span>->ids = <span style="color: #800080;">$result</span>-><span style="color: #000000;">fetch_all(MYSQLI_ASSOC);
    }<br>
  <span style="color: #008000;">/*<span style="color: #008000;">*<br>   * 返回当前元素<br>   <span style="color: #008000;">*/</span></span></span>
   </span><span style="color: #0000ff;">function</span> <span style="color: #008080;">current</span><span style="color: #000000;">(){
        </span><span style="color: #800080;">$id</span> = <span style="color: #800080;">$this</span>->ids[<span style="color: #800080;">$this</span>->index]['ID'<span style="color: #000000;">];
        </span><span style="color: #0000ff;">return</span> Factory::getHacl(<span style="color: #800080;">$id</span><span style="color: #000000;">);
   }<br>
  <span style="color: #000000;"><span style="color: #008000;">/*<span style="color: #008000;">*<br>   * 向前移动到下一个元素<br>   <span style="color: #008000;">*/</span></span></span></span> 
    </span><span style="color: #0000ff;">function</span> <span style="color: #008080;">next</span><span style="color: #000000;">(){
        </span><span style="color: #800080;">$this</span>->index ++<span style="color: #000000;">;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 返回到迭代器的第一个元素
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span> <span style="color: #008080;">rewind</span><span style="color: #000000;">(){
        </span><span style="color: #800080;">$this</span>->index = 0<span style="color: #000000;">;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 查询当前位置是否有数据
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">function</span><span style="color: #000000;"> valid(){
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->index - <span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">ids);
    }<br>
  <span style="color: #000000;"><span style="color: #008000;">/*<span style="color: #008000;">*<br>   * 返回当前元素的键<br>   <span style="color: #008000;">*/</span></span></span></span>
    </span><span style="color: #0000ff;">function</span> <span style="color: #008080;">key</span><span style="color: #000000;">(){
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">index;
    }
}</span>

index.php

<span style="color: #800080;">$hacls</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> \Baobab\AllHacl();
</span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$hacls</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$hacl</span><span style="color: #000000;">){
    </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$hacl</span>-><span style="color: #000000;">haclname);
}</span>

Hacl类相关内容参考数据对象映射模式。http://www.cnblogs.com/tianxintian22/p/5232016.html

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),