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.