PHP设计模式 迭代器模式,php设计模式模式
迭代器模式,在不需要了解内部实现的前提下,遍历一个聚合对象的内部元素。相比于传统的编程模式,迭代器模式可以隐藏遍历元素所需要的操作。
AllHacl.php
<?<span>php namespace Baobab; </span><span>class</span> AllHacl <span>implements</span><span> \iterator{ </span><span>protected</span> <span>$ids</span><span>;</span><span>protected</span> <span>$index</span>;<span>//</span><span>当前位置</span> <span>function</span><span> __construct(){ </span><span>$db</span> = Factory::getDatabase('ha_cl'<span>); </span><span>$result</span> = <span>$db</span>->query('select ID from ha_cl'<span>); </span><span>$this</span>->ids = <span>$result</span>-><span>fetch_all(MYSQLI_ASSOC); }<br /> <span>/*<span>*<br /> * 返回当前元素<br /> <span>*/</span></span></span> </span><span>function</span> <span>current</span><span>(){ </span><span>$id</span> = <span>$this</span>->ids[<span>$this</span>->index]['ID'<span>]; </span><span>return</span> Factory::getHacl(<span>$id</span><span>); }<br /> <span><span>/*<span>*<br /> * 向前移动到下一个元素<br /> <span>*/</span></span></span></span> </span><span>function</span> <span>next</span><span>(){ </span><span>$this</span>->index ++<span>; } </span><span>/*</span><span>* * 返回到迭代器的第一个元素 </span><span>*/</span> <span>function</span> <span>rewind</span><span>(){ </span><span>$this</span>->index = 0<span>; } </span><span>/*</span><span>* * 查询当前位置是否有数据 </span><span>*/</span> <span>function</span><span> valid(){ </span><span>return</span> <span>$this</span>->index - <span>count</span>(<span>$this</span>-><span>ids); }<br /> <span><span>/*<span>*<br /> * 返回当前元素的键<br /> <span>*/</span></span></span></span> </span><span>function</span> <span>key</span><span>(){ </span><span>return</span> <span>$this</span>-><span>index; } }</span>
index.php
<span>$hacls</span> = <span>new</span><span> \Baobab\AllHacl(); </span><span>foreach</span>(<span>$hacls</span> <span>as</span> <span>$hacl</span><span>){ </span><span>var_dump</span>(<span>$hacl</span>-><span>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
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks agoByDDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment