PHP迭代器模式的定义是什么,怎样实现
Admin 2022-09-23 群英技术资讯 1422 次浏览
这篇文章将为大家详细讲解有关“PHP迭代器模式的定义是什么,怎样实现”的知识,下文有详细的介绍,小编觉得挺实用的,对大家学习或工作或许有帮助,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在深入研究这个设计模式之前,我们先来看一道面试题,来自鸟哥的博客,
题目是这样的:
使对象可以像数组一样进行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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP中的重载是什么意思,如何应用呢?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
本文给大家介绍什么是RPC,怎么用PHP简单快速的创建一个RPC服务,其实很简单哦,一起来看看吧~希望对需要的朋友有所帮助~
php框架的整理和补充:1、Akelos,是基于MVC设计模式的框架;2、Fusebox,构建ColdFusion和PHPWeb应用最受欢迎的框架;3、Php On Trax采用MVC结构;4、PHPDevShell 是开源的应用开发框架。
位运算符允许对整型数中指定的位进行求值和操作。位运算符例子名称结果$a&$bAnd(按位与)将把 $a 和 $b 中都为1的位设为1。$a|$bOr(按位同...
PHP中的异常处理机制是比较难理解的一个知识点,很多朋友在学习过程中,对于异常处理有些不明白。为了帮助大家更好的学习和理解PHP中的异常处理机制,文本会详细介绍异常处理机制,有这方面学习需求的朋友可以看一下。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008