php spl库的使用(PHP标准库)【摘抄引用】
文章来源与推荐阅读:阮一峰--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界面
* 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界面
- SeekableIterator界面
- Countable界面
- 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类
- ArrayIterator类
- RecursiveArrayIterator类和RecursiveIteratorIterator类
- FilterIterator
- SimpleXMLIterator类
- CachingIterator类
- LimitIterator类
- SplFileObject类
php spl库的使用(PHP标准库)【摘抄引用】的更多相关文章
- ca72a_c++_标准IO库:面向对象的标准库
/*ca72a_c++_标准IO库:面向对象的标准库继承:基类->派生类3个头文件9个标准库类型IO对象不可复制或赋值 ofstream, f--file,文件输出流ostringstream, ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- c++标准库
Technical Report 1不是正式的库只是一个草案,作为C++ 2003标准的附加库被大多数编译器厂商所支持,它是个过渡性质的库,其实现将会作为C++11标准的一部分.很多编译器对C++11 ...
- 【python】标准库的大致认识
正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...
- Python标准库——走马观花
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python有一套很有用的标准库(standard library).标准库会随着 ...
- c标准库和运行时库
c运行时库与c标准库的区别 c标准库包括常用的数学函数.字符串操作函数等等,这些函数都是由编程高手写的,效率高,很少出错,而且是完全符合c语言标准的函数. c运行库可以说是c标准库的扩展集,它是完全包 ...
- C++标准库和标准模板库
转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...
- C++著名类库和C++标准库介绍
C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...
- 16.C++-初探标准库
在别人代码里,经常看到std命名空间,比如使用std命名空间里的标准输入输出流对象cout: #include<iostream> using namespace std; int mai ...
- C++标准库和标准模板库(转)
转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...
随机推荐
- linux ln 建立软链接-- 基于dubbo-zookeeper服务的 服务jar 引用公共的 lib
对于ln命令网上有很多的教程,这里不再复述, 其基本目的是:多个文件夹公用一个文件夹的里的文件. 其基本命令格式: ln [option] source_file dist_file (source_ ...
- C#的特性Attribute
一.什么是特性 特性是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签,这个标签可以有多个.您可以通过使用特性向程序添加声明性信息.一个声明性标签是通过放置在它 ...
- struts2官方 中文教程 系列十一:使用XML进行表单验证
在本教程中,我们将讨论如何使用Struts 2的XML验证方法来验证表单字段中用户的输入.在前面的教程中,我们讨论了在Action类中使用validate方法验证用户的输入.使用单独的XML验证文件让 ...
- Error: Error while compiling statement: FAILED: SemanticException Unable to determine if hdfs://hadoopNode2:8020/user/hive/warehouse/test is encrypted...
1.发现问题: 在hive客户端或者beeline查询hive表时候报错: 根据报错信息查看,是在集群namenode做了HA之后,产生的hdfs路径不对的问题: 2.解决问题,修改hive元数据my ...
- 通过py2exe打包python程序的过程中,解决的一系列问题
py2exe的使用方法参考<py2exe使用方法>. 注:程序可以在解释器中正常运行,一切问题都出在打包过程中. 问题1: 现象:RuntimeError: maximum recursi ...
- IDLE激活方法
激活流程 一.通过Activation code 方式激活, 注册码获取地址为:http://idea.lanyus.com/ 在idea或者pycharm的Activation code中输入 注册 ...
- 『MongoDB』集合更新操作
参考 定义 db.collection.update(query, update, options) 改变一个在集合中已经存在的文档或文档数组.默认的,update()方法更新一个独立的文档.如果mu ...
- js滚动及可视区域的相关的操作
element.getBoundingClientRect 判断指定元素相对于页面可视窗口的位置信息,通常结合windows.onScroll方法使用,当element.getBoundingClie ...
- ES5新增数组方法(1):filter
检测数组元素,并返回符合条件所有元素的数组. 1.过滤数组中不符合条件的元素 let arr = [1, 2, 3, 4, 5, 6]; // 方式一 let newArr = arr.filter( ...
- 利用selenium自动化登录淘宝
#encoding=utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import A ...