: This article mainly introduces the design mode entry-iterator mode (php version). If you are interested in the PHP Tutorial, please refer to it. Before going into this design model, let's take a look at a interview question, from the blog of laruence,
The question is as follows:
So that the object can perform a foreach loop like an array, requiring the attribute to be private.
It is difficult to implement it without using the iterator mode. First, let's look at the implementation code:
Sample. php
_arr = $arr;}public function current(){ return current($this->_arr);}public function next(){ return next($this->_arr);}public function key(){ return key($this->_arr);}public function valid(){ return $this->current() !== false;}public function rewind(){ reset($this->_arr);}}
Index. php
$v){ echo $k."-".$v."
";}
The Iterator interface comes from the spl class library of php. after writing the article on the design mode, we will further study this class library.
In addition, I found a yii Framework implementation code about the iterator mode on the Internet:
class CMapIterator implements Iterator {/*** @var array the data to be iterated through*/ private $_d;/*** @var array list of keys in the map*/ private $_keys;/*** @var mixed current key*/ private $_key; /*** Constructor.* @param array the data to be iterated through*/ public function __construct(&$data) { $this->_d=&$data; $this->_keys=array_keys($data); } /*** Rewinds internal array pointer.* This method is required by the interface Iterator.*/ public function rewind() { $this->_key=reset($this->_keys); } /*** Returns the key of the current array element.* This method is required by the interface Iterator.* @return mixed the key of the current array element*/ public function key() { return $this->_key; } /*** Returns the current array element.* This method is required by the interface Iterator.* @return mixed the current array element*/ public function current() { return $this->_d[$this->_key]; } /*** Moves the internal pointer to the next array element.* This method is required by the interface Iterator.*/ public function next() { $this->_key=next($this->_keys); } /*** Returns whether there is an element at current position.* This method is required by the interface Iterator.* @return boolean*/ public function valid() { return $this->_key!==false; }} $data = array('s1' => 11, 's2' => 22, 's3' => 33);$it = new CMapIterator($data);foreach ($it as $row) { echo $row, '
';}
The official definition of the iterator design pattern is to use the Iterator pattern to provide unified access to aggregate objects, that is, to provide an external iterator to access and traverse aggregate objects, the internal structure of the object does not need to be exposed. It is also called the Cursor mode.
Well, I don't quite understand. Why can we use foreach to traverse arrays? we need to use this iterator mode for further understanding.
Reference:
Http://www.cnblogs.com/davidhhuan/p/4248206.html
Http://blog.csdn.net/hguisu/article/details/7552841
Http://www.phppan.com/2010/04/php-iterator-and-yii-cmapiterator/
PHP source code reading notes 24: iterator implementation when the value is false can not complete the reason for iteration analysis: http://www.phppan.com/2010/04/php-source-24-iterator-false-value/
The above introduces the design mode entry-iterator mode (php version), including some content, hope to be helpful to friends who are interested in PHP tutorials.