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

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

May 20, 2016 am 11:54 AM

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

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

 

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

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)