文章来源与推荐阅读阮一峰--PHP SPL笔记  &&  PHP SPL使用方法和他的威力

1.SPL 是什么?

  • SPL:standard php library php标准库,此 从php5.0起开始内置的组件和接口,在5.3以后逐渐成熟。因为内置在php5开发环境中,无需任何配置。

   根据官方定义,“a collection of interfaces and classes that are meant to solve standard problems.” 然而在目前的使用者,spl更多地被看做是一种使object模仿的array行为的interfaces和classes。

  • SPL对PHP引擎进行了扩展,例如ArrayAccess、Countable和SeekableIterator等接口,它们用于以数组形式操作对象。同时还可以使用RecursiveIterator,ArrayObjects等其他迭代器进行数组的迭代操作。

     他还内置了几个对象,例如Exceptions,SplObserver,spltorage以及splautoloadregister,splclasses,iteratorapply等的帮助函数,用于重载对应的功能。

2.Iterator

  • spl的核心概念是Iterator,这指一种设计模式(Design Pattern),"provide an object which traverses some aggregate structure,abstracting away assumptions about the implementation of that structure." 通俗的说,Iterator能够使许多不同的数据结构,都能有统一的操作界面,比如一个数据库的结果集、同一目录的文件集或者一个文本中每一行构成的集合。
  • SPL规定,所有部署了Iterator界面的class,都可以用在foreach loop中。Iterator界面包含以下必须部署的五个方法:
* current()

      This method returns the current index's value. You are solely
responsible for tracking what the current index is as the
interface does not do this for you. * key() This method returns the value of the current index's key. For
foreach loops this is extremely important so that the key
value can be populated. * next() This method moves the internal index forward one entry. * rewind() This method should reset the internal index to the first element. * valid() This method should return true or false if there is a current
element. It is called after rewind() or next().
  • ArrayAccess界面
  部署ArrayAccess界面,可以使object像Array那样操作,但是必须包含四个必须部署的方法
 
 * offsetExists($offset)

      This method is used to tell php if there is a value
for the key specified by offset. It should return
true or false. * offsetGet($offset) This method is used to return the value specified
by the key offset. * offsetSet($offset, $value) This method is used to set a value within the object,
you can throw an exception from this function for a
read-only collection. * offsetUnset($offset) This method is used when a value is removed from
an array either through unset() or assigning the key
a value of null. In the case of numerical arrays, this
offset should not be deleted and the array should
not be reindexed unless that is specifically the
behavior you want.
  • IteratorAggregate界面
  • RecursiveIterator界面
这个界面用于遍历多层数据,继承了Iterator界面,因而也具有标准的current()/key()/next()和valid()方法。同时它自己还规定了getChildren()和hasChildren()方法。
  • SeekableIterator界面
SeekableIterator界面也是Iterator界面的延伸,除了Iterator的五个方法以外,还规定了seek()方法,参数是元素的位置,返回该元素。若该位置不存在,则抛出OutOfBoundsException。
  • Countable界面
这个界面规定了一个count()方法,返回结果集的数量
 
3.SPL Classes
  • spl内置类
  查看所有内置类 
foreach(spl_classes() as $key=>$val){
echo $key."=>".$val.'<br/>';
}
  • DirectoryIterator类
        这个类用来查看一个目录中所有文件和子目录
foreach(new DirectoryIterator('./') as $Item)
{
echo $Item.'<br/>';
}
catch(Exception $e)
{
echo 'No files Found!';
}
  • ArrayObject类
        此类将Array转换为Object
  • ArrayIterator类
        这个类实际上是对ArrayObject类的补充,为后者提供遍历功能。也支持offset类方法和count()方法
  • RecursiveArrayIterator类和RecursiveIteratorIterator类
        ArrayIterator类和ArrayObject类,只支持遍历一维数组,如果要遍历多维数组,必须先用RecursiveIteratorIterator生成一个Iterator,然后再对这个Iterator使用RecursiveIteratorIterator
  • FilterIterator
        FilterIterator类可以对元素进行过滤,只要在accept()方法中设置过滤条件就可以了。
  • SimpleXMLIterator类
        这个类用来遍历xml文件
  • CachingIterator类
        这个类有一个hasNext()方法,用来判断是否还有下一个元素
  • LimitIterator类
        这个类用来限定返回结果集的数量和位置,必须提供offset和limit两个参数,与SQL命令中的limit语句类似
  • SplFileObject类
        这个类用来对文本文件进行遍历
 
 
 

php spl库的使用(PHP标准库)【摘抄引用】的更多相关文章

  1. ca72a_c++_标准IO库:面向对象的标准库

    /*ca72a_c++_标准IO库:面向对象的标准库继承:基类->派生类3个头文件9个标准库类型IO对象不可复制或赋值 ofstream, f--file,文件输出流ostringstream, ...

  2. 【转】C++标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...

  3. c++标准库

    Technical Report 1不是正式的库只是一个草案,作为C++ 2003标准的附加库被大多数编译器厂商所支持,它是个过渡性质的库,其实现将会作为C++11标准的一部分.很多编译器对C++11 ...

  4. 【python】标准库的大致认识

    正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...

  5. Python标准库——走马观花

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python有一套很有用的标准库(standard library).标准库会随着 ...

  6. c标准库和运行时库

    c运行时库与c标准库的区别 c标准库包括常用的数学函数.字符串操作函数等等,这些函数都是由编程高手写的,效率高,很少出错,而且是完全符合c语言标准的函数. c运行库可以说是c标准库的扩展集,它是完全包 ...

  7. C++标准库和标准模板库

    转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...

  8. C++著名类库和C++标准库介绍

    C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...

  9. 16.C++-初探标准库

    在别人代码里,经常看到std命名空间,比如使用std命名空间里的标准输入输出流对象cout: #include<iostream> using namespace std; int mai ...

  10. C++标准库和标准模板库(转)

    转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...

随机推荐

  1. 【python3.X】python练习笔记[1]

    ##三位数水仙花 ##方法一,小于指定数字的水仙花数 x=eval(input()) a,b,c = 0,0,0 for i in range (100,x,1): a=i%10 b=i//100 c ...

  2. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  3. php长整型完整输出

    今天调用webservice时返回一个字段是int64 长整型 原始的数值应该是 190000002101056096 而php返回时转成 1.9000000210106E+17 当传入另一个接口就报 ...

  4. c#根据ip获取城市地址

    用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...

  5. javac、jar使用实录

    因项目管理部署需要,记录一下过程,以免下次忘记了,再次使用又需要重头再来,只记录正确的操作方式,可能会提到某些错误 建立项目所在目录F:\www 案例一 其下建立项目的java源文件的包目录结构.ja ...

  6. Jmeter——小性能用例

    1.添加默认值,将代理服务器写入 2.添加HTTP请求头,将域名部分用变量形式写入:${__CSVRead(D:/number.txt,0)},这是为了查询不同页面,在D:/number.txt路径下 ...

  7. 【Selenium-Python】Selenium-Firefox 环境配置 win64

    Python 已安装完毕 Selenium 安装: Windows > cmd pip install selenium 注:未加selenium版本号时默认安装最新版本. 查询当前Seleni ...

  8. Python简要标准库(5)

    hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 基本的生成MD密匙的函数 import hashlib md5 = hashlib.md5() md5.up ...

  9. 【java并发编程实战】第八章:线程池的使用

    1.线程饥饿锁 定义:在线程池中,如果任务的执行依赖其他任务,那么可能会产生线程饥饿锁.尤其是单线程线程池. 示例: public class ThreadDeadStarveTest { publi ...

  10. android:保存用户名密码等应用程序数据

    转自http://blog.sina.com.cn/s/blog_a73687bc0101dsjj.html (一)使用SharedPreferences  1.保存信息: SharedPrefere ...