首页 > 编程知识 正文

php迭代器的内部执行过程详解,php迭代器的内部执行过程详解

时间:2023-12-27 22:27:38 阅读:326444 作者:JQHG

本文目录一览:

在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.com

* @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.com

* @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迭代器iterator怎么用

使用foreach 与使用迭代器,并不冲突 

迭代器可以使用在:

1、使用返回迭代器的包或库时(如PHP5中的SPL迭代器)

2、无法在一次的调用获取容器的所有元素时

3、要处理数量巨大的无素时(数据库中的表以GB计的数据)

迭代器还可以用来构造一些数据结构。

你可以去后盾人平台看看,里面的东西不错

迭代器是什么

迭代器(iterator)有时又称游标(cursor)是程序设计的软件设计模式,可在容器(container,例如链表或阵列)上遍访的接口,设计人员无需关心容器的内容。

迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。然而迭代器有很多不同的能力,它可以把抽象容器和通用算法有机的统一起来。

迭代器提供一些基本操作符:*、++、==、!=、=。这些操作和C/C++“操作array元素”时的指针接口一致。不同之处在于,迭代器是个所谓的复杂的指针,具有遍历复杂数据结构的能力。其下层运行机制取决于其所遍历的数据结构。因此,每一种容器型都必须提供自己的迭代器。事实上每一种容器都将其迭代器以嵌套的方式定义于内部。因此各种迭代器的接口相同,型号却不同。这直接导出了泛型程序设计的概念:所有操作行为都使用相同接口,虽然它们的型别不同。

功能

迭代器使开发人员能够在类或结构中支持foreach迭代,而不必整个实现IEnumerable或者IEnumerator接口。只需提供一个迭代器,即可遍历类中的数据结构。当编译器检测到迭代器时,将自动生成IEnumerable接口或者IEnumerator接口的Current,MoveNext和Dispose方法。

特点

1.迭代器是可以返回相同类型值的有序序列的一段代码;

2.迭代器可用作方法、运算符或get访问器的代码体;

3.迭代器代码使用yieldreturn语句依次返回每个元素,yield break将终止迭代;

4.可以在类中实现多个迭代器,每个迭代器都必须像任何类成员一样有惟一的名称,并且可以在foreach语句中被客户端,代码调用如下所示:foreach(int x in SimpleClass.Iterator2){};

5.迭代器的返回类型必须为IEnumerable和IEnumerator中的任意一种;

6.迭代器是产生值的有序序列的一个语句块,不同于有一个 或多个yield语句存在的常规语句块;

7.迭代器不是一种成员,它只是实现函数成员的方式,理解这一点是很重要的,一个通过迭代器实现的成员,可以被其他可能或不可能通过迭代器实现的成员覆盖和重载;

8.迭代器块在C#语法中不是独特的元素,它们在几个方面受到限制,并且主要作用在函数成员声明的语义上,它们在语法上只是语句块而已;

9.yield关键字用于指定返回的值。到达yieldreturn语句时,会保存当前位置。下次调用迭代器时将从此位置重新开始执行。 迭代器对集合类特别有用,它提供一种简单的方法来迭代不常用的数据结构(如二进制树)。

php:我是Php新手下面这个数组排序看不大懂,求高手指点啊

$users 内部有一个迭代器。

reset 使这个迭代器重新开始迭代,

key 取得这个迭代器的当前位置上的键(即数组的下标)

next 将这个迭代器移动到下一个位置。

这两个循环是一样的,通过使用迭代器对数组遍历,并逐个输出元素的内容。

谁能帮我讲解一下这段php PDO的代码

1 RecursiveArrayIterator迭代器

这个迭代器允许设置和修改值和键时遍历数组和对象在相同的方式,ArrayIterator。此外,可以迭代当前迭代器条目。

2 LEAVES_ONLY  只迭代当前叶子节点

mode

Optional mode. Possible values are

RecursiveIteratorIterator::LEAVES_ONLY - The default. Lists only leaves in iteration.

RecursiveIteratorIterator::SELF_FIRST - Lists leaves and parents in iteration with parents coming first.

RecursiveIteratorIterator::CHILD_FIRST - Lists leaves and parents in iteration with leaves coming first.

3 迭代器对象 已经设置好表格样式

4 迭代器对象循环输出显示

current 迭代器当前指向的叶子结点

总结  :  TableRows 类继承 RecursiveArrayIterator ,重新设置每个children 与 叶子结点 的左右样式(这里表格),然后通过sql语句,查找数据库,循环迭代器对象输出显示。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。