PHP Design Pattern iterator mode

Source: Internet
Author: User
An iterator pattern that iterates through the inner elements of an aggregated object without needing to know the internal implementation. In contrast to traditional programming patterns, the iterator pattern hides the operations required to traverse elements.

allhacl.php

 Phpnamespace Baobab;classAllhaclImplements\iterator{protected$ids;protected$index;//Current Positionfunction__construct () {$db= Factory::getdatabase (' ha_cl '); $result=$db->query (' Select ID from Ha_cl ')); $this->ids =$result-Fetch_all (MYSQLI_ASSOC); }
/**
* Returns the current element
*/function Current(){ $id=$this->ids[$this->index][' ID ']; returnFactory::gethacl ($id); }
/**
* Move forward to the next element
*/functionNext(){ $this->index + +; } /** * Returns to the first element of an iterator*/functionRewind(){ $this->index = 0; } /** * Query the current location for data*/functionvalid () {return$this->index-Count($this-IDs); }
/**
* Returns the key of the current element
*/functionKey(){ return$this-index; }}

index.php

$hacls New \baobab\allhacl (); foreach ($haclsas$hacl) {    var_dump($hacl-haclname) ;}

Hacl class-related content Reference data object mapping mode. Http://www.cnblogs.com/tianxintian22/p/5232016.html

The above describes the PHP design pattern iterator mode, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.