Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-05-20 11:54:53
Original
1689 Leute haben es durchsucht

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

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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

 

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage