很多站长朋友们都不太清楚php迭代器模式,今天小编就来给大家整理php迭代器模式,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php设计模式介绍之迭代器模式 2、 php迭代器iterator怎么用 3、 foreach和迭代器的区别 php 4、 在PHP中遍历对象用什么? php设计模式介绍之迭代器模式《PHP设计模式介绍》第八章 迭代器模式
类中的面向对象编程封装应用逻辑 类 就是实例化的对象 每个单独的对象都有一个特定的身份和状态 单独的对象是一种组织代码的有用方法 但通常你会处理一组对象或者集合
属性来自 SQL 查询的一组数据就是一个集合 就像本书前面章节介绍的 Monopoly 游戏示例的对象列表
集合不一定是均一的 图形用户界面框架中的 Window 对象可以收集任意数量的控制对象 - Menu Slider 和 Button 并且 集合的实现可以有多种方式 PHP 数字是一个集合 但也是一个散列表 一个链接列表 一个堆栈以及队列
问题
如何操纵任意的对象集合?
解决方案
使用迭代器模式来提供对集合内容的统一存取
你可能没有意识到这一点 但你每天都在使用迭代器模式 - 它潜藏在 PHP 的数组类型和各种数组操作函数中 (其实 给你一些固有类的数组的组合和一群用这些固有类工作的可变函数 你将不得不使用这些数组来处理对象集合 这是在 PHP 中的本地数组迭代
$test? =? array( one ? o ? three );$output? =? ; reset($test); do? {$output? =? current($test);}? while? (next($test));echo? $output;? //? produces? oneothree
reset() 函数将迭代重新转到数组的开始 current() 返回当前元素的值 next() 则前进至数组中的下一个元素并返回新的 current() 值 当你超出数组的最后一个元素时 next() 返回 false 使用这些迭代方法 PHP 数组的内部实现就与你不相关了 迭代器结合了封装和多态的面向对象程序设计原理 使用迭代器 你可以对集合中的对象进行操作 而无需专门了解集合如何显现或者集合包含什么(对象的种类) 迭代器提供了不同固定迭代实现的统一接口 它完全包含了如何操纵特定集合的详细信息 包括显示哪些项(过滤)及其显示顺序(排序)
让我们创建一个简单的对象 在数组中对它进行操作 (尽管该示例在 PHP 环境下 但迭代器并不特定于 PHP 虽然添加了较多的引用操作符 本章节中的大多数示例在 PHP 下也能够运行) 对象 Lendable 表示诸如电影 相册等媒体 它作为 Web 站点的一部分或服务 允许用户浏览或将他们的媒体集合分享给其他用户 (对??????? 于该示例 请无需考虑其他方面 )让我们开始下面对 Lendable 基础设计的测试
//? PHP class? LendableTestCase? extends? UnitTestCase? {function? TestCheckout()? {$item? =? new? Lendable;$this >assertFalse($item >borrower);$item >checkout( John );$this >assertEqual( borrowed ? $item >status);$this >assertEqual( John ? $item >borrower);}function? TestCheckin()? {$item? =? new? Lendable;$item >checkout( John );$item >checkin();$this >assertEqual( library ? $item >status);$this >assertFalse($item >borrower);}}
要实现这一最初测试的需求 我们来创建一个带有若干公共属性和一些方法的类 来触发这些属性的值
class? Lendable? {public? $status? =? library ;public? $borrower? =? ;public? function? checkout($borrower)? {$this >status? =? borrowed ;$this >borrower? =? $borrower;}public? function? checkin()? {$this >status? =? library ;$this >borrower? =? ;}}
Lendable 是一个好的 普通的开端 让我们将它扩展到诸如 DVD 或 CD 的磁道项 媒体扩展了 Lendable 并且磁道详细记录了特定媒体的详细信息 包括项目的名称 发布的年份以及项本身的类型
class? Media? extends? Lendable? {public? $name; public? $type; public? $year;public? function? __construct($name ? $year ? $type= dvd )? {$this >name? =? $name;$this >type? =? $type;$this >year? =? (int)$year;}}
要使事情更加简单 媒体有三个公共的实例变量 Media::name Media::year 和Media::type 构造函数采用了两个参数 将第一个存储在 $name 中 第二个存储在 $year 中 构造函数还允许可选的第三个参数来指定类型(缺省为dvd)
给定单独的对象来操作 你现在可以创建一个容器来包含他们 Library 类似于常用的库 Library 应该能够添加 删除和计算集合中的项 甚至 Library 还应该允许访问集合(本章中的样本代码部分可看到示例)中的单一的项(对象)
我们开始构建 Library 的测试用例
class? LibraryTestCase? extends? UnitTestCase? {function? TestCount()? {$lib? =? new? Library;$this >assertEqual( ? $lib >count());}}
它是满足这一测试的简单类
class? Library? {function? count()? {return? ;}}
继续将一些有趣的功能添加到测试中
class? LibraryTestCase? extends? UnitTestCase? {function? TestCount()? {? /*? ? */? }function? TestAdd()? {$lib? =? new? Library;$lib >add( one );$this >assertEqual( ? $lib >count());}}
实现 add() 的简单方法是建立在 PHP 灵活数组函数的基础上 你可以将项添加到实例变量并使用 count() 来返回集合众项的数量
class? Library? {protected? $collection? =? array();function? count()? {return? count($this >collection);}function? add($item)? {$this >collection[]? =? $item;}}
lishixinzhi/Article/program/net/201311/13092
php迭代器iterator怎么用使用foreach 与使用迭代器,并不冲突?
迭代器可以使用在:
1、使用返回迭代器的包或库时(如PHP5中的SPL迭代器)
2、无法在一次的调用获取容器的所有元素时
3、要处理数量巨大的无素时(数据库中的表以GB计的数据)
迭代器还可以用来构造一些数据结构。
你可以去后盾人平台看看,里面的东西不错
foreach和迭代器的区别 php迭代器是一种更高级的工具。foreach是简单的循环语法。虽然功能上看起来相似。但迭代器是工具,这是二者性质上的不同,所以迭代器有更丰富的功能特性,还可以自定义具体的实现。特别是在内存占用上,迭代器是按需读取数据,foreach是一次性载入数据。PHP里面PDO,SimpleXML里面都有迭代器的具体实现,更完整的全部在SPL扩展部分。
在PHP中遍历对象用什么?其实百度一下就知道
我们知道,php中,foreach可以很方便地对可迭代结构(例如数组,再如对象)进行迭代操作:
[php] view plaincopy
foreach( $array as $elem){
var_dump($elem);
}
[php] view plaincopy
foreach($obj as $key=>$value){
echo "$key=>$value".PHP_EOL;
}
因而我们想:如果对于一个实例化对象,对其进行foreach操作,会发生什么事情呢?
首先我们定义的基础类为:
[php] view plaincopy
Class Test{
/* one public variable */
public $a;
public $b;
/* one private variable */
private $c;
public function __construct(){
$this->a = "public";
$this->b = "public";
$this->c = "private";
}
public function traverseInside(){
foreach($this as $key=>$value){
echo $key."=>".$value.EOL;
}
}
}
然后我们实例化该类,对其进行迭代,并与内部迭代的结果进行比较:
[php] view plaincopy
$test = new Test;
echo "<hr>";
echo "traverse outside:".EOL;
foreach( $test as $key=>$value ){
echo $key."=>".$value.EOL;
}
echo "<hr>";
echo "traverse inside:".EOL;
$test->traverseInside();
迭代的结果为:
可以看出:外部foreach循环的结果,只是将对象的公有属性(public)循环出来了,而对于私有属性(private),外部foreach是无法循环出来的。因而我们如果想要在外部通过foreach循环出类的所有的属性(公有的和私有的),仅仅依靠foreach是不行的,必须要对类进行“改造”。如何对类进行改造呢?如果你了解foreach的实现(参考laruence的博客:),那么可以很轻松地找到相应的方案。另外一方面,《设计模式-可复用面向对象软件设计的基础》中也提到:通过将对象的访问和遍历从对象中分离出来并放入一个迭代器对象中,迭代器模式可以实现以不同的方式对对象进行遍历。我们暂时不去深挖这句话的意思,只要知道,使用迭代器可以对对象进行遍历即可。
PHP手册<预定义接口>部分指出:要实现迭代器模式,需要在可迭代对象中实现如下接口:
[php] view plaincopy
abstractpublicmixedcurrent( void )
abstractpublicscalarkey( void )
abstractpublicvoidnext( void )
abstractpublicvoidrewind( void )
abstractpublicbooleanvalid( void )
有了这个。实现迭代器模式就很方便了,一个简单的实例如下:
[php] view plaincopy
class TestIterator implements Iterator {
private $point = 0;
private $data = array(
"one","two","three",
);
public function __construct() {
$this->point = 0;
}
function rewind() {
$this->point = 0;
}
function current() {
return $this->data[$this->point];
}
function key() {
return $this->point;
}
function next() {
++$this->point;
}
function valid() {
return isset($this->data[$this->point]);
}
}
$it = new TestIterator;
foreach($it as $key => $value) {
echo $key, $value;
echo "\n";
}
当然,使用了迭代器的对象可以以如下方式进行遍历:
[php] view plaincopy
$it = new TestIterator;
$it->rewind();
while ($it->valid()){
$key = $it->key();
$value = $it->current();
echo "$key=>$value";
$it->next();
}
最后附上YII中ListIterator(顾名思义,实现对List的迭代操作的迭代器)的实现:
[php] view plaincopy
<?php
/**
* CListIterator class file.
*
* @author Qiang Xue <qiang.xue@gmail测试数据>
* @link
* @copyright Copyright ? 2008-2011 Yii Software LLC
* @license
*/
/**
* CListIterator implements an interator for {@link CList}.
*
* It allows CList to return a new iterator for traversing the items in the list.
*
* @author Qiang Xue <qiang.xue@gmail测试数据>
* @version $Id$
* @package system.collections
* @since 1.0
*/
class CListIterator implements Iterator
{
/**
* @var array the data to be iterated through
*/
private $_d;
/**
* @var integer index of the current item
*/
private $_i;
/**
* @var integer count of the data items
*/
private $_c;
/**
* Constructor.
* @param array $data the data to be iterated through
*/
public function __construct($data)
{
$this->_d=$data;
$this->_i=0;
$this->_c=count($this->_d);
}
/**
* Rewinds internal array pointer.
* This method is required by the interface Iterator.
*/
public function rewind()
{
$this->_i=0;
}
/**
* Returns the key of the current array item.
* This method is required by the interface Iterator.
* @return integer the key of the current array item
*/
public function key()
{
return $this->_i;
}
/**
* Returns the current array item.
* This method is required by the interface Iterator.
* @return mixed the current array item
*/
public function current()
{
return $this->_d[$this->_i];
}
/**
* Moves the internal pointer to the next array item.
* This method is required by the interface Iterator.
*/
public function next()
{
$this->_i++;
}
/**
* Returns whether there is an item at current position.
* This method is required by the interface Iterator.
* @return boolean
*/
public function valid()
{
return $this->_i<$this->_c;
}
}
关于php迭代器模式的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php迭代器模式 迭代器iterator remove的详细内容...