Home  >  Article  >  php教程  >  PHP设计模式 迭代器模式 - tianxintian22

PHP设计模式 迭代器模式 - tianxintian22

WBOY
WBOYOriginal
2016-05-20 11:54:531693browse

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

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