PHP設計模式 迭代器模式,php設計模式模式_PHP教程

來源:互聯網
上載者:User

PHP設計模式 迭代器模式,php設計模式模式


迭代器模式,在不需要瞭解內部實現的前提下,遍曆一個彙總對象的內部元素。相比於傳統的編程模式,迭代器模式可以隱藏遍曆元素所需要的操作。

AllHacl.php

phpnamespace Baobab;class AllHacl implements \iterator{    protected $ids;protected $index;//當前位置    function __construct(){        $db = Factory::getDatabase('ha_cl');        $result = $db->query('select ID from ha_cl');        $this->ids = $result->fetch_all(MYSQLI_ASSOC);    }
  /**
   * 返回當前元素
   */ function current(){ $id = $this->ids[$this->index]['ID']; return Factory::getHacl($id); }
  /**
   * 向前移動到下一個元素
   */ function next(){ $this->index ++; } /** * 返回到迭代器的第一個元素 */ function rewind(){ $this->index = 0; } /** * 查詢當前位置是否有資料 */ function valid(){ return $this->index - count($this->ids); }
  /**
   * 返回當前元素的鍵
   */ function key(){ return $this->index; }}

index.php

$hacls = new \Baobab\AllHacl();foreach($hacls as $hacl){    var_dump($hacl->haclname);}

Hacl類相關內容參考資料對象映射模式。http://www.cnblogs.com/tianxintian22/p/5232016.html

http://www.bkjia.com/PHPjc/1110970.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1110970.htmlTechArticlePHP設計模式 迭代器模式,php設計模式模式 迭代器模式,在不需要瞭解內部實現的前提下,遍曆一個彙總對象的內部元素。相比於傳統的編...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.