900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 迭代器模式及其php实现(Yii框架)

迭代器模式及其php实现(Yii框架)

时间:2019-08-16 12:15:09

相关推荐

迭代器模式及其php实现(Yii框架)

后端开发|php教程

php,迭代器模式,Yii框架

后端开发-php教程

迭代器模式是一种行为型模式,它是一种最简单也最常见的设计模式。它可以让使用者透过特定的接口巡访容器中的每一个元素而不用了解底层的实际操作。

企业协同办公系统源码,ubuntu系统关机不了,爬虫箱上等座,php==非,seo蚂蚁论坛lzw

适用性

开单大师php源码企业版,ubuntu是什么英文,招投标网站爬虫,php apcahe,seo客户服务lzw

在希望利用语言本身的遍历函数便利自定义结构时,例如PHP中的foreach函数

微信广告植入系统源码,vscode设置自动保存,ubuntu环境变量,如何访问tomcat,sqlite驱动,东莞 网页设计,远程数据库连接,qq企业邮箱pop服务器和smtp服务器,jquery1.11 插件,前端框架技术,java爬虫视频,php静态,SEO网站公司,创建springboot,jquery下拉列表数据标签,网站图片布局,本地网页搜索引擎代码,java 数据库登录注册模板,最新73种暴利产品竞价单页网站制作带订单后台系统模板,手机导航切换页面,花卉销售管理系统,wap和微信小程序lzw

类图

PHP实例

_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); }}// client$data = array(1, 2, 3, 4, 5);$sa = new sample($data);foreach ($sa AS $key => $row) { echo $key, , $row,

;}?>

在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,

;}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。