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>
Copy after login
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>
Copy after login
Hacl类相关内容参考数据对象映射模式。http://www.cnblogs.com/tianxintian22/p/5232016.html
Statement of this Website
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

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

Hot Article
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD
Nordhold: Fusion System, Explained
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
Java Tutorial
1664
14


CakePHP Tutorial
1423
52


Laravel Tutorial
1321
25


PHP Tutorial
1269
29


C# Tutorial
1249
24

