SPL学习 迭代器
主要学习内容:
慕课网的spl视频教程
阮一峰SPL学习笔记 http://www.ruanyifeng.com/blog/2008/07/php_spl_notes.html
SPL类详解 http://www.cnblogs.com/ellisonDon/archive/2013/02/26/2932978.html
PHP SPL,被遗落的宝石 http://www.nowamagic.net/librarys/veda/detail/2165
SPL 迭代器:
SPL更多地被看作是一种使object(物体)模仿array(数组)行为的interfaces和classes。
SPL的内容除了这些模仿的的接口和类,还包括了一些具体功能的内置迭代器对象。
1、对象模仿数组,或者拓宽数组的功能。
两个根,ArrayAccess 和 Traversable
ArrayAccess 叫数组式操作,就是[]的使用。
Traversable 是迭代器的主要的接口,但不能直接使用。
生成迭代器的两个接口,都是继承于Traversable
IteratorAggregate 对一个非迭代器对象,用函数getIterator返回迭代器
Iterator 迭代器,最主要
这四个接口在php属于语言层面,用来获得基本的SPL迭代器
//内部不使用数组,实现迭代器
class Fibonacci implements Iterator {
private $previous = 0;
private $current = 1;
private $key = 0; public function current() {
return $this->current;
} public function key() {
return $this->key;
} public function next() {
// 关键在这里
// 将当前值保存到 $newprevious
$newprevious = $this->current;
// 将上一个值与当前值的和赋给当前值
$this->current += $this->previous;
// 前一个当前值赋给上一个值
$this->previous = $newprevious;
$this->key++;
} public function rewind() {
$this->previous = 1;
$this->current = 0;
$this->key = 0;
} public function valid() {
if($this->key>12) return false;
return true;
}
} //内部用数组构建一个迭代器,同时实现了 数组式访问,随即跳转,count计数
class arrFibonacci implements Iterator, ArrayAccess, SeekableIterator, Countable{
private $arr = array(0,1);
private $pos = 0; //循环 Iterator
public function current() {
return $this->arr[$this->pos];
} public function key() {
return $this->pos;
} public function next() {
$this->pos++;
if($this->pos > 1 && !isset($this->arr[$this->pos]) ){
$this->arr[$this->pos] = $this->arr[$this->pos-2] + $this->arr[$this->pos-1];
}
} public function rewind() {
$this->pos = 0;
} public function valid() {
if($this->pos>=15) return false;
return isset($this->arr[$this->pos]);
} //数组式访问元素 ArrayAccess
function offsetExists($offset) {
return isset($this->arr[$offset]);
} function offsetGet($offset){
return $this->arr[$offset];
} function offsetSet($offset, $value){
if(isset($this->arr[$offset]))
$this->arr[$offset] = $value;
} function offsetUnset($offset){
unset( $this->arr[$offset] );
} //跳转 SeekableIterator
function seek($position) {
echo $position,'<br>';
if ( isset($this->arr[$position]) ) {
$this->pos = $position;
}
} //计数
function count(){
return count( $this->arr );
} //显示,为了验证,不需要对应SPL接口
public function show(){
showarr($this->arr);
}
} // ArrayAccess 让一个类具有数组式访问模式
class Article implements ArrayAccess {
public $title;
public $author;
public $category; function __construct($title,$author,$category) {
$this->title = $title;
$this->author = $author;
$this->category = $category;
} function offsetExists($key) {
return array_key_exists($key,get_object_vars($this)); //get_object_vars — 返回由对象属性组成的关联数组
} function offsetSet($key, $value) {
if ( array_key_exists($key,get_object_vars($this)) ) {
$this->{$key} = $value;
}
} function offsetGet($key) {
if ( array_key_exists($key,get_object_vars($this)) ) {
return $this->{$key};
}
} function offsetUnset($key) {
if ( array_key_exists($key,get_object_vars($this)) ) {
unset($this->{$key});
}
} } // 让一个类 返回一个迭代器
class arrArticle implements IteratorAggregate {
public $arr = array(); function __construct($title,$author,$category) {
$this->arr['title'] = $title;
$this->arr['author'] = $author;
$this->arr['category'] = $category;
} //可以用 IteratorAggregate 接口 返回一个 Iterator
function getIterator() {
return new ArrayIterator( $this->arr );
} }
SPL迭代器的功能,继承于Iterator的接口,扩展迭代器功能
Countable 用内部函数count,为PHP count 函数 提供结果
SeekableIterator 用函数seek来进行跳转 Iterator 数据的当前位置
RecursiveIterator 递归迭代器,需要实现两个函数,hasChildren()判断当前数据是否可以迭代,和getChildren()获得这个子迭代器
OuterIterator 多重迭代器,迭代器包含子迭代器,(注意和IteratorAggregate不同)。可以用函数 getInnerIterator 返回子迭代器,也可以直接使用这个外部迭代器,其自动迭代子迭代器,中间可以做一定更改,功能强大,用途广泛,SPL中很多内置的迭代器实现了这个接口。还有一个实现的类,IteratorIterator ,可以直接使用这个类。
FilterIterator 抽象类,过滤
AppendIterator 连接多个迭代器 串联
MultipleIterator 合并多个迭代器 并联
2、内置迭代器对象
ArrayIterator 数组迭代器 可用于将PHP数组转换为迭代器,然后用于其他内置的迭代器进行处理。
SPL学习 迭代器的更多相关文章
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- php SPL学习
数据结构 SplDoublyLinkedList - 该SplDoublyLinkedList类提供了一个双向链表的主要功能 SplStack - 该SplStack类提供了一种使用双向链表实现栈的主 ...
- ###STL学习--迭代器
点击查看Evernote原文. #@author: gr #@date: 2014-08-23 #@email: forgerui@gmail.com STL中的迭代器. ###stl学习 |--迭代 ...
- Python学习---迭代器学习1210
可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的generator fun ...
- python学习------迭代器协议和生成器
一.递归和迭代 递归:自己调用自己 举例解释:问路 A问B康明网络科技怎么走,B说我不是很清楚,我帮你问问C,C说我也不知道.我问问D,D说 就在兴隆.之后D返回结果给C,C返回结果给B,B返回结 ...
- python学习-迭代器,列表解析和列表生成式
迭代器为类序列对象提供了一个类序列的接口.Python 的迭代无缝的支持序列对象,而且还允许程序猿迭代非序列类型,包括用户定义的对象. 迭代器是一个next()方法的对象,而不是通过索引计数.当需要下 ...
- Python学习——迭代器&生成器&装饰器
一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退迭代器的一大优点是不要求事先准备好整个迭代过程中所有的元素.迭代器仅 ...
- day13 python学习 迭代器,生成器
1.可迭代:当我们打印 print(dir([1,2])) 在出现的结果中可以看到包含 '__iter__', 这个方法,#次协议叫做可迭代协议 包含'__iter__'方法的函数就是可迭代函数 ...
- 学习迭代器实现C#异步编程——仿async/await(一)
.NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C ...
随机推荐
- 【Inno Setup】 Inno Setup 64位安装程序默认安装路径
在脚本中加入: ArchitecturesInstallIn64BitMode=x64 ArchitecturesAllowed=x64
- chrome下的js调试
console输出对象信息:console.log(object[,object,.....])
- android开发系列之友盟统计集成
相比大家都遇到这种情况,当我们的app上线之后,我们想要实时的跟踪了解到app里面的bug情况.新增用户情况.用户相关的行为属性情况等.但是如果自己在app里面去开发集成这些功能,一方面开发工作量还挺 ...
- ECMAScript5
张鑫旭:ECMAScript5介绍 淘宝整理的es5-safe /********* It provides the following methods: Function.prototype.bin ...
- 管理员必备的20个Linux系统监控工具
需要监控Linux服务器系统性能吗?尝试下面这些系统内置或附件的工具吧.大多数Linux发行版本都装备了大量的监控工具.这些工具提供了能用作取得相关信息和系统活动的量度指标.你能使用这些工具发现造成性 ...
- libevent简介 构成
libevent简介 libevent是一个事件驱动的网络库,支持跨平台,如Linux, *BSD, MacOS X, Solaris, Windows.支持I/O多路复用,epoll.poll./d ...
- iOS10推送必看UNNotificationServiceExtension
转:http://www.cocoachina.com/ios/20161017/17769.html (收录供个人学习用) iOS10推送UNNotificationServic 招聘信息: 产品经 ...
- iOS 取得单张系统图片
这里主要用到了UIImagePickerController 不多废话,直接上代码 // // RootViewController.m // GetImageFromPhotoAlbum // // ...
- SqlServer中Sql语句的逻辑执行顺序
准备数据 Sql脚本如下,两张表,一张客户表Customers只包含customerid和city字段,一张订单表Orders包含orderid和customerid(关联Customers的cust ...
- Abstract Class与 Interface 的区别
表格 Abs ...