Home  >  Article  >  Backend Development  >  PHP design pattern iterator pattern, php design pattern pattern_PHP tutorial

PHP design pattern iterator pattern, php design pattern pattern_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:57:02898browse

PHP design pattern Iterator pattern, PHP design pattern pattern

Iterator pattern traverses the internal elements of an aggregate object without knowing the internal implementation. Compared with traditional programming patterns, the iterator pattern can hide the operations required to traverse elements.

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>

For information related to the Hacl class, please refer to the data object mapping mode. http://www.cnblogs.com/tianxintian22/p/5232016.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1110970.htmlTechArticlePHP design pattern iterator pattern, php design pattern pattern iterator pattern, without knowing the internal implementation , traverse the internal elements of an aggregate object. Compared with traditional knitting...
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