官方网站建设专业公司,wordpress+国内不使用,网站设计视频,规划管理部门的网站建设迭代器#xff08;Iterator#xff09;模式#xff0c;又叫做游标#xff08;Cursor#xff09;模式。GOF给出的定义为#xff1a;提供一种方法访问一个容器#xff08;container#xff09;对象中各个元素#xff0c;而又不需暴露该对象的内部细节。 百度百科: http:… 迭代器Iterator模式又叫做游标Cursor模式。GOF给出的定义为提供一种方法访问一个容器container对象中各个元素而又不需暴露该对象的内部细节。 百度百科: http://baike.baidu.com/view/9791023.htm?fraladdin 解释 上面这名话可能多数人看得似懂非懂什么叫做访问容器的各个元素又不暴露对象的内部细节呢尤其是网上很多例子都过于简单直接扔一个数组然后去实现了迭代器的各种方法如下 ?php class SomeCollection implements Iterator { protected $_data; protected $_pos; function __construct($data) { $this-_data $data; $this-_pos 0; } function current() { $row $this-_data[$this-_pos]; return $row; } function next() { $this-_pos; } function valid() { return $this-_pos 0 $this-_pos count($this-_data); } function key() { return $this-_pos; } function rewind() { $this-_pos 0; } } $array array( array(url www.zeroplace.cn), array(url www.baidu.com), array(url www.sina.com.cn), array(url www.google.com), array(url www.qq.com), ); $coll new SomeCollection($array); foreach ($coll as $row) { echo $row[url], \n; } 这样的例子就不能够说明迭代器的作用因为它不能说明迭代器隐藏了内部的数据结构传进去的和返回出来的完全是一样的数据。 迭代器怎么用 我只能说在不同的场合有不同的用法。比如我把上面的例子修改一下可能就可以说明迭代器可以隐藏数据结构这个特性了。请看如下代码。 ?php class SomeCollection implements Iterator { protected $_data; protected $_pos; function __construct($data) { $this-_data $data; $this-_pos 0; } function current() { $row $this-_data[$this-_pos]; $row[ip] gethostbyname($row[url]); return $row; } function next() { $this-_pos; } function valid() { return $this-_pos 0 $this-_pos count($this-_data); } function key() { return $this-_pos; } function rewind() { $this-_pos 0; } } $array array( array(url www.zeroplace.cn), array(url www.baidu.com), array(url www.sina.com.cn), array(url www.google.com), array(url www.qq.com), ); $coll new SomeCollection($array); foreach ($coll as $row) { echo $row[url], , $row[ip], \n; } 这样我觉得就可以说明迭代器能隐藏数据结构这个特性了。我们的数据传进去的时候每行数据只有一个url属性但是迭代出来的时候多了一个ip属性。这样对外部的使用者来说就是有两个属性(url和ip), 它不需要知道这个ip字段是创建者传入的还是在迭代器中产生的。 更一般的做法 这里current方法返回的是一个关联数组更常规的做法是返回一个对象此时这个迭代器可能还需要一个对象创建器。转载于:https://www.cnblogs.com/agang-php/p/5909983.html