PHP迭代器模式简介
本内容主要介绍PHP语言中迭代器模式的简易构建方式,以及Yii框架内对迭代器的具体实现机制,供读者参考学习。
关于迭代器的概念,可参考维基百科的相关定义。
迭代器(Iterator),亦称光标(cursor),是一种程序设计中的软件设计模式。它提供了在容器对象(如list或vector)上进行遍历访问的统一接口,使得设计者无需关注容器内部的具体数据结构。
不同编程语言对Iterator的实现方式存在差异。部分面向对象语言如Java、C#、Python和Delphi已将Iterator特性内置于语言核心中,实现了与语言的完美整合,这类被称为隐式迭代器(implicit iterator)。而像C++这样的语言本身未内置Iterator特性,但其标准模板库(STL)仍利用模板技术实现了功能强大的迭代器。
此外,Iterator还可以与Generator进行整合。在某些语言中二者被视为同一接口,而在另一些语言中则被独立区分。 参考地址:http://zh.wikipedia.org/zh-cn/%E8%BF%AD%E4%BB%A3%E5%99%A8
Iterator简单实现与缺陷
【Iterator的简单实现】
/**
* Iterator模式的简单实现类
*/
class sample implements Iterator {
private $_items ;
public function __construct(&$data) {
$this->_items = $data;
}
public function current() {
return current($this->_items);
}
public function next() {
next($this->_items);
}
public function key() {
return key($this->_items);
}
public function rewind() {
reset($this->_items);
}
public function valid() {
return ($this->current() !== FALSE);
}
}
/** DEMO */
$data = array(1, 2, 3, 4, 5);
$sa = new sample($data);
foreach ($sa AS $key => $row) {
echo $key, ' ', $row, '
';
}
在编写next()方法时曾产生过困惑,起初误认为该方法需要返回下一个元素的值。
这种误解源于将next()方法与普通的next函数混淆,但实际上并非如此。
查阅官方手册可知其具体定义。
abstract public void Iterator::next ( void )
根据文档说明,其返回值类型明确为void。
因此,在该方法内部直接调用next函数即可,无需进行额外的返回值处理。
然而,上述简易实现在处理特定类型的数组时会暴露出缺陷。
$data = array('0' => 11, "" => 22, 's3' => 33, 0, 0, "", false, 0, 1);
执行以下代码后的输出结果如下:
0 11 22 s3 33 1 0 2 0 3
可以看到,当遇到false值后,后续的元素未能继续迭代显示,具体原因尚待探究。
Yii框架中提供的迭代器实现有效规避了这一缺陷。
Yii框架CMapIterator解决方案
【Yii框架中的迭代器实现】
在Yii框架的代码库中可以观察到其迭代器的具体实现逻辑。
位于collections目录下的CMapIterator.php文件包含了相关实现代码。
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, '
';
}
与前述简单实现相比,Yii版本通过控制key的变化来移动位置。这种设计的核心目的在于避免当数组值包含false时导致迭代提前终止的问题。
希望上述关于Yii框架下PHP程序设计的分析能对读者有所帮助。