PHP迭代器模式的定义是什么,怎样实现
Admin 2022-09-23 群英技术资讯 818 次浏览
在深入研究这个设计模式之前,我们先来看一道面试题,来自鸟哥的博客,
题目是这样的:
使对象可以像数组一样进行foreach循环,要求属性必须是私有。
不使用迭代器模式很难实现,先看实现的代码:
sample.php
<?php class Sample implements Iterator{ private $_arr; public function __construct(Array $arr){ $this->_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
<?php require 'Sample.php'; $arr = new Sample(['max', 'ben', 'will']); foreach ($arr as $k=>$v){ echo $k."-".$v."<br />"; }
其中Iterator接口来自php的spl类库,在写完设计模式的相关文章之后,将会进一步研究这个类库。
另外在网上找到了一段yii框架中关于迭代器模式的实现代码:
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 />'; }
关于迭代器设计模式官方的定义是:使用迭代器模式来提供对聚合对象的统一存取,即提供一个外部的迭代器来对聚合对象进行访问和遍历 , 而又不需暴露该对象的内部结构。又叫做游标(Cursor)模式。
好吧,我不是很能理解。为什么明明数组已经可以用foreach来遍历了还要用这样一种迭代器模式来实现,只有等待工作经验的加深来进一步理解吧。
关于“PHP迭代器模式的定义是什么,怎样实现”的内容今天就到这,感谢各位的阅读,大家可以动手实际看看,对大家加深理解更有帮助哦。如果想了解更多相关内容的文章,关注我们,群英网络小编每天都会为大家更新不同的知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
nginx和swoole的区别:Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Swoole是一个面向生产环境的 PHP 异步网络通信引擎。
这篇文章主要介绍了php装饰者模式简单应用,结合具体实例形式分析了php装饰者模式的原理及文章编辑相关应用操作技巧,需要的朋友可以参考下
在说明名称解析规则之前,我们先看一些重要的定义:命名空间名称定义非限定名称名称中不包含命名空间分隔符的标识符,例如 Foo限定名称名称中含有命名空间分隔符的...
在开发过程中,有时候我们会需要在PHP函数在运行Linux系统代码,那么我们就会使用到这些函数,shell_exec函数,exec函数,system函数,passthru函数,popen函数,proc_open函数。下面我们就来详细了解下PHP执行linux命令可能会用到的六个函数。
swoole不能访问的解决办法:首先使用命令“yum install -y openssl”安装openssl;然后安装swoole;接着通命令“php-config --enable-openssl”进行编译即可正常访问。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008