PHP中迭代器的简单实现及Yii框架中的迭代器实现方法示例


Posted in PHP onApril 26, 2020

本文实例讲述了PHP中迭代器的简单实现及Yii框架中的迭代器实现方法。分享给大家供大家参考,具体如下:

在维基百科中我们可以看到其定义如下:

迭代器有时又称光标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如list或vector)上遍访的接口,设计人员无需关心容器物件的内容。

各种语言实作Iterator的方式皆不尽同,有些面向对象语言像Java, C#, Python, Delphi都已将Iterator的特性内建语言当中,完美的跟语言整合,我们称之隐式迭代器(implicit iterator),但像是C++语言本身就没有Iterator的特色,但STL仍利用template实作了功能强大的iterator。

Iterator另一方面还可以整合Generator。有些语言将二者视为同一接口,有些语言则将之独立化。
地址:http://zh.wikipedia.org/zh-cn/%E8%BF%AD%E4%BB%A3%E5%99%A8

【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, '<br />';
}

在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框架中的迭代器实现】

在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, '<br />';
}

这与之前的简单实现相比,其位置的变化是通过控制key来实现的,这种实现的作用是为了避免false作为数组值时无法迭代

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php下用GD生成生成缩略图的两个选择和区别
Apr 17 PHP
php学习笔记 面向对象的构造与析构方法
Jun 13 PHP
解析thinkphp的左右值无限分类
Jun 20 PHP
ThinkPHP3.1新特性之Action参数绑定
Jun 19 PHP
destoon二次开发模板及调用语法汇总
Jun 21 PHP
PHP合并静态文件详解
Nov 14 PHP
PHP Hash算法:Times33算法代码实例
May 13 PHP
php获取远程文件内容的函数
Nov 02 PHP
Zend Framework教程之Zend_Registry对象用法分析
Mar 22 PHP
PHP 布尔值的自增与自减的实现方法
May 03 PHP
PHP利用递归函数实现无限级分类的方法
Mar 22 PHP
PDO实现学生管理系统
Mar 21 PHP
PHP设计模式之迭代器模式Iterator实例分析【对象行为型】
Apr 26 #PHP
Yii Framework框架开发微信公众平台示例
Apr 26 #PHP
PHP随机生成中文段落示例【测试网站内容时使用】
Apr 26 #PHP
PHP过滤器 filter_has_var() 函数用法实例分析
Apr 23 #PHP
PHP优化之批量操作MySQL实例分析
Apr 23 #PHP
Thinkphp 框架扩展之Widget扩展实现方法分析
Apr 23 #PHP
Thinkphp 框架扩展之行为扩展原理与实现方法分析
Apr 23 #PHP
You might like
人族 Terran 魔法与科技
2020/03/14 星际争霸
深入for,while,foreach遍历时间比较的详解
2013/06/08 PHP
修改apache配置文件去除thinkphp url中的index.php
2014/01/17 PHP
PHP中执行cmd命令的方法
2014/10/11 PHP
PHP验证信用卡卡号是否正确函数
2015/05/27 PHP
linux下为php添加iconv模块的方法
2016/02/28 PHP
php爬取天猫和淘宝商品数据
2018/02/23 PHP
理解Javascript_13_执行模型详解
2010/10/20 Javascript
jquery mobile changepage的三种传参方法介绍
2013/09/13 Javascript
浅谈javascript中call()、apply()、bind()的用法
2015/04/20 Javascript
jQuery网页右侧广告跟随滚动代码分享
2020/04/20 Javascript
AngularJS中实现动画效果的方法
2016/07/28 Javascript
JS判断时间段的实现代码
2017/06/14 Javascript
jQuery实现菜单栏导航效果
2017/08/15 jQuery
探索webpack模块及webpack3新特性
2017/09/18 Javascript
微信小程序wx.request实现后台数据交互功能分析
2017/11/25 Javascript
JS实现图片转换成base64的各种应用场景实例分析
2018/06/22 Javascript
Bootstrap标签页(Tab)插件切换echarts不显示问题的解决
2018/07/13 Javascript
vue实现分页的三种效果
2020/06/23 Javascript
[01:08:29]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第一场 1月9日
2021/03/11 DOTA
关于python pyqt5安装失败问题的解决方法
2017/08/08 Python
基于Python log 的正确打开方式
2018/04/28 Python
对pandas里的loc并列条件索引的实例讲解
2018/11/15 Python
Python饼状图的绘制实例
2019/01/15 Python
Python子类继承父类构造函数详解
2019/02/19 Python
Python中三元表达式的几种写法介绍
2019/03/04 Python
Django保护敏感信息的方法示例
2019/05/09 Python
详解python pandas 分组统计的方法
2019/07/30 Python
Django2.1.7 查询数据返回json格式的实现
2020/12/29 Python
意大利折扣和优惠券网站:Groupalia
2019/10/09 全球购物
会计系毕业生求职信
2014/05/28 职场文书
承诺书模板
2014/08/30 职场文书
2014年十一国庆节活动方案
2014/09/16 职场文书
物业保洁员岗位职责
2015/02/13 职场文书
爱国电影观后感
2015/06/19 职场文书
Mysql中存储引擎的区别及比较
2021/06/04 MySQL