SPL是Standard PHP Library(PHP标准库)的缩写。

根据官方定义,它是"a collection of interfaces and classes that are meant to solve standard problems"。但是,目前在使用中,SPL更多地被看作是一种使object(物体)模仿array(数组)行为的interfaces和classes。

这几天学习SPL,这是我写的一点小笔记,望日后再次使用时能够想起点滴,SPL核心便是Iterator,熟练使用Iterator可以让我们更加方便的去操作数据.

ArrayIterator迭代器示例

 <?php
$fruits = array
(
"apple" => 'apple value',
"orange" => 'orange value',
"grape" => "grape value",
"plum" => "plum value"
);
print_r($fruits);
echo "**** use fruits directly\n";
foreach ($fruits as $key => $value)
{
echo $key . ":" . $value . "\n";
}
//使用ArrayIterator遍历数组
$obj = new ArrayObject($fruits);
$it = $obj->getIterator(); echo "***** use ArrayIterator in for\n";
foreach ($it as $key => $value)
{
echo $key . ":" . $value . "\n";
} echo "***** use ArrayIterator in while\n";
$it->rewind();//调用current之前要用rewind
while ($it->valid())
{
echo $it->key() . ":" . $it->current()."\n";
$it->next();
}
//跳过某些元素进行打印
echo "***** use seek before while\n";
$it->rewind();
if ($it->valid())
{
$it->seek(1);
while($it->valid())
{
echo $it->key() . ":" . $it->current()."\n";
$it->next();
}
} echo "***** use ksort\n";
$it->ksort();
foreach ($it as $key => $value)
{
echo $key . ":" . $value . "\n";
} echo "***** use asort\n";
$it->asort();
foreach ($it as $key => $value)
{
echo $key . ":" . $value . "\n";
}

ArrayIterator

AppendIterator迭代器示例

 <?php
$array_a = new ArrayIterator(array('a','b','c'));
$array_b = new ArrayIterator(array('d','e','f'));
$it = new AppendIterator();
$it->append($array_a);
//通过append方法把迭代器对象添加到AppendIterator对象中
$it->append($array_b);
foreach ($it as $key => $value)
{
echo $value."\n";
}

AppendIterator

MultipleIterator迭代器示例

 <?php
$idIter = new ArrayIterator(array('01','02','03'));
$nameIter = new ArrayIterator(array('张三','李四','王五'));
$ageIter = new ArrayIterator(array('22','23','25'));
$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mit->attachIterator($idIter,"ID");
$mit->attachIterator($nameIter,"NAME");
$mit->attachIterator($ageIter,"AGE");
foreach ($mit as $value)
{
print_r($value);
}

MultipleIterator

FilesystemIterator迭代器示例

 <?php
$it = new FilesystemIterator('.');
foreach ($it as $finfo)
{
printf("%s\t%s\t%8s\t%s\n",
date("Y-m-d H:i:s",$finfo->getMTime()),
$finfo->isDir()?"<DIR>":"",
number_format($finfo->getSize()),
$finfo->getFileName()
);
}

FilesystemIterator

Countable接口示例

 <?php
class CountMe implements Countable
{
protected $_myCount = 3;
public function count()
{
return $this->_myCount;
}
}
$obj = new CountMe();
echo count($obj);

Countable

OuterIterator接口示例

 <?php
$array = ['Value1','Value2','Value3','Value4'];
$outerObj = new OuterImpl(new ArrayIterator($array)); foreach ($outerObj as $key => $value)
{
echo "++" . $key . " - " . $value . "\n";
} class OuterImpl extends IteratorIterator
{
public function current()
{
return parent::current()."_tail";
}
public function key()
{
return "Pre_".parent::key();
}
}

OuterIterator

autoload机制的三种用法

 <?php
//设置autoload寻找php定义的类文件的扩展名,多个扩展名用逗号分隔,前面的扩展名优先被匹配
spl_autoload_extensions('.class.php,.php');
//设置autoload寻找php定义的类文件的目录,多个目录用PATH_SEPARATOR进行分隔
set_include_path(get_include_path().PATH_SEPARATOR."libs/");
//提示PHP使用Autoload机制查找类定义
spl_autoload_register();
new Test();

autoload01

 <?php
//定义__autoload函数,可以在不调用spl_autoload_register函数的情况完成类的装载
function __autoload($class_name)
{
echo "__autoload class:".$class_name."\n";
require_once ($class_name.'.php');
}
//定义一个替换__autoload函数的类文件装载函数
function classLoader($class_name)
{
echo "classLoader() load class:".$class_name."\n";
require_once ($class_name.'.php');
}
//传入定义好的装载类的函数名称替换__autoload
spl_autoload_register('classLoader');
new Test();

autoload02

 <?php

 //定义一个替换__autoload函数的类文件装载函数
function classLoader($class_name)
{
echo "classLoader() load class:".$class_name."\n";
set_include_path(".");
spl_autoload($class_name);//当我们不用require或者requir_once载入类文件的时候,而想通过系统查找include_path来装载类是,必须显式调用spl_autoload函数,参数是类的名称来重启类文件的自动查找(装载)
}
//传入定义好的装载类的函数名称替换__autoload
spl_autoload_register('classLoader');
new Test();

autoload03

SPL的基本使用的更多相关文章

  1. PHP 高级编程(4/5) - SPL异常类之 LogicException 逻辑异常

    SPL 提供了一系列标准异常.日常的使用中我们应该根据需求科学的使用它们,来使我们的程序更加健壮.LogicException 是从 Exception 基类派生的,没有添加任何附加方法.抛出逻辑异常 ...

  2. PHP 高级编程(3/5) - 使用SPL(标准PHP库)实现观察者模式

    SPL(标准PHP库 - Standard PHP Library)是PHP5面向对象功能中重要的部分.原文解释是这样的“The Standard PHP Library (SPL) is a col ...

  3. u-boot-2015.04 在tq2440上的移植(使用spl引导u-boot)

    本次移植跟以往的不同之处是采用了spl来引导u-boot,参考了博客http://blog.csdn.net/fulinus/article/details/42738641 下载链接:http:// ...

  4. PHP SPL(PHP 标准库)

    一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...

  5. 【夯实PHP基础】PHP标准库 SPL

    PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...

  6. U-boot的目录结构及spl功能

    转 http://tieba.baidu.com/p/2836672721 对uboot-2010.06及其以后的版本,将体系结构相关的内容合并,增加include文件夹,分离出通用库文件lib,其各 ...

  7. PHP标准库 (SPL) 笔记

    简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...

  8. php SPL学习

    数据结构 SplDoublyLinkedList - 该SplDoublyLinkedList类提供了一个双向链表的主要功能 SplStack - 该SplStack类提供了一种使用双向链表实现栈的主 ...

  9. PHP 标准库 SPL 之数据结构栈(SplStack)简单实践

    PHP 5.3.0 版本及以上的堆栈描述可以使用标准库 SPL 中的 SplStack class,SplStack 类继承双链表 ( SplDoublyLinkedList ) 实现栈. 代码: & ...

  10. PHP 设计模式 笔记与总结(3)SPL 标准库

    SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...

随机推荐

  1. 将已有项目导入Gitlab

    登陆GitLab,创建添加项目. 写入项目的基本信息,包括名称.描述.权限等等. cd existing_folder git init git remote add origin git@10.10 ...

  2. const参数,const返回值与const函数

    在C++程序中,经常用const 来限制对一个对象的操作,例如,将一个变量定义为const 的: const  int  n=3; 则这个变量的值不能被修改,即不能对变量赋值. const 这个关键字 ...

  3. [ofbiz]screen中应用form和ftl,控制页面元素属性

    可以在screen中定义form与ftl两个文件,ftl中可以使用js控制form中的页面元素属性. 控制元素是否可编辑:        $("#oaDataReport_budget&qu ...

  4. c#接口定义与应用

    public interface IBankAccount //只能加public修饰符,或者什么都不加 { void Playin(decimal money); //函数前不加任何修饰符号 boo ...

  5. SurfaceView的补充

    1.什么时候使用:当自定义View需要频繁刷新,或者刷新数据比较大的时候,建议使用SurfaceView取代使用View 2.继承SurfaceView的并重写的步骤:①.继承SurfaceView类 ...

  6. django接收和发送json数据

    通过json.jumps处理字典数据, 发送给前端 def get_context_data(self, **kwargs): ctx = super(HelpUpdateView, self).ge ...

  7. C语言--基本运算符

    一.算术运算符 1. 加法运算符 + * 除了可以进行加法运算外,还可以表示正号:+521 2.减法运算符 — * 除了可以进行减法运算外,还可以表示负号:—741 3.乘法运算法符 * * 请注意符 ...

  8. PHP设计模式之装饰器模式

    装饰器模式:如果已有对象的部分内容或功能性发生改变,但是不需要修改原始对象的结构或不使用继承,动态的扩展一个对象的功能,则应该使用装饰器模式.简单点说:就是我们不应该去修改已有的类,而是通过创建另外一 ...

  9. webshell 匿名用户(入侵者)

    “web”的含义是显然需要服务器开放web服务,“shell”的含义是取得对服务器某种程度上操作权限.webshell常常被称为匿名用户(入侵者)通过网站端口对网站服务器的某种程度上操作的权限.由于w ...

  10. 从Linux终端管理进程:10个你必须知道的命令

    从Linux终端管理进程:10个你必须知道的命令 Linux终端有一系列有用的命令.它们可以显示正在运行的进程.杀死进程和改变进程的优先级.本文列举了一些经典传统的命令和一些有用新颖的命令.本文提到的 ...