Home >Backend Development >PHP Tutorial >PHP design pattern iterator pattern

PHP design pattern iterator pattern

WBOY
WBOYOriginal
2016-08-08 09:20:58834browse

1. Concept introduction

1. Iterator pattern: Traverse the internal elements of an aggregate object without knowing the internal implementation.
2. Compared with traditional programming patterns, the iterator pattern can hide the operations required to traverse elements.
3. The iterator introduced here needs to implement the Iterator in PHP SPL and needs to implement 5 methods (current, next, valid, rewid, key)

2. Code display

<code><span>namespace</span><span>brave</span><span>class</span><span>AllUser</span><span>implements</span> \<span>Iterator</span>
{
    //所有<span>user</span>的<span>id</span><span>protected</span> $<span>ids</span>;
    <span>//保存数据库查询的对象,如果有就不需要在次查询了,可使用注册模式</span><span>protected</span><span>$data</span> = <span>array</span>(); 
    <span>//表示迭代器当前的位置</span><span>protected</span><span>$index</span>;

    <span><span>function</span><span>__construct</span><span>()</span>
    {</span><span>$db</span> = Factory::getDatabase();
        <span>$result</span> = <span>$db</span>->query(<span>"select id from user"</span>);
        <span>$this</span>->ids = <span>$result</span>->fetch_all(MYSQLI_ASSOC);
    }

    <span>//获取当前用户对象</span><span><span>function</span><span>current</span><span>()</span>
    {</span><span>$id</span> = <span>$this</span>->ids[<span>$this</span>->index][<span>'id'</span>];
        <span>return</span> Factory::getUser(<span>$id</span>);
    }

    <span>//进入下一个索引</span><span><span>function</span><span>next</span><span>()</span>
    {</span><span>$this</span>->index ++;
    }

    <span>//检查当前是否有数据</span><span><span>function</span><span>valid</span><span>()</span>
    {</span><span>return</span><span>$this</span>->index < count(<span>$this</span>->ids);
    }

    <span>//使当前的指针回到开始位置</span><span><span>function</span><span>rewind</span><span>()</span>
    {</span><span>$this</span>->index = <span>0</span>;
    }

    <span>//获取当前的索引值</span><span><span>function</span><span>key</span><span>()</span>
    {</span><span>return</span><span>$this</span>->index;
    }

}</code>

3. Execute the code

<code><span>//迭代器模式实例</span><span>$users</span> = <span>new</span> AllUser();
<span>foreach</span> (<span>$users</span><span>as</span><span>$user</span>) {
    var_dump(<span>$user</span>);
    <span>echo</span><span>'<hr>'</span>;
}</code>

Copyright Statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.

The above has introduced the iterator pattern of PHP design pattern, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:webserver two modesNext article:webserver two modes