SPL的基本使用
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的基本使用的更多相关文章
- PHP 高级编程(4/5) - SPL异常类之 LogicException 逻辑异常
SPL 提供了一系列标准异常.日常的使用中我们应该根据需求科学的使用它们,来使我们的程序更加健壮.LogicException 是从 Exception 基类派生的,没有添加任何附加方法.抛出逻辑异常 ...
- PHP 高级编程(3/5) - 使用SPL(标准PHP库)实现观察者模式
SPL(标准PHP库 - Standard PHP Library)是PHP5面向对象功能中重要的部分.原文解释是这样的“The Standard PHP Library (SPL) is a col ...
- u-boot-2015.04 在tq2440上的移植(使用spl引导u-boot)
本次移植跟以往的不同之处是采用了spl来引导u-boot,参考了博客http://blog.csdn.net/fulinus/article/details/42738641 下载链接:http:// ...
- PHP SPL(PHP 标准库)
一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...
- 【夯实PHP基础】PHP标准库 SPL
PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...
- U-boot的目录结构及spl功能
转 http://tieba.baidu.com/p/2836672721 对uboot-2010.06及其以后的版本,将体系结构相关的内容合并,增加include文件夹,分离出通用库文件lib,其各 ...
- PHP标准库 (SPL) 笔记
简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...
- php SPL学习
数据结构 SplDoublyLinkedList - 该SplDoublyLinkedList类提供了一个双向链表的主要功能 SplStack - 该SplStack类提供了一种使用双向链表实现栈的主 ...
- PHP 标准库 SPL 之数据结构栈(SplStack)简单实践
PHP 5.3.0 版本及以上的堆栈描述可以使用标准库 SPL 中的 SplStack class,SplStack 类继承双链表 ( SplDoublyLinkedList ) 实现栈. 代码: & ...
- PHP 设计模式 笔记与总结(3)SPL 标准库
SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...
随机推荐
- 部署sharepointform验证
1 iis sharepoint v4 提供程序2 web service 验证提供程序3 创建web应用程序(选择基于身份验证模式,配置提供程序)4 创建网站集(空)5 创建网站集
- 查看Android数据库文件
使用Eclipse DDMS的File Explorer不能能看到Data目录下面的东西,可能由下面原因引起的 手机没有Root -- 可以使用Root大师 Data文件权限不够 -- 可以使用Ro ...
- JavaScript 原型链的一点想法
JavaScript借鉴了许多语言的特点:例如语法类Java.函数借鉴Scheme.原型继承借鉴自Self.正则表达式借鉴于Perl.(DC Javascript:语言精粹). 首先,每个J ...
- HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)
若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans + ...
- Candy Bags
读懂了题就会发现这是个超级大水题 Description Gerald has n younger brothers and their number happens to be even. One ...
- 动态链接库的生成(dll)和 动态链接库隐式and显式调用
一.构建动态链接库(dll.dll dll.lib dll.h) 说明: .dll 是在执行程序是调用 .lib 是在连接程序是调用 .h是在编译程序时调用 1.头文件(声明导入函数):_decl ...
- 设计师如何为 Android 应用标注尺寸
http://blog.cutterman.cn/?p=33 1. 画布大小定位 720 x 1280,72 dpi2. 只使用偶数单位的尺寸,比如 96 px 的列表项高度,16 px 的边距,64 ...
- http status 400,http 400,400 错误
转载:http://blog.csdn.net/xu_zh_h/article/details/2294233 4 请求失败4xx 4xx应答定义了特定服务器响应的请求失败的情况.客户端不应当在不更改 ...
- KeyEvent
http://blog.csdn.net/elfylin/article/details/8008763 一. 接口KeyEvent.Callback和View.OnKeyListener 二. 流程 ...
- QT多重继承的时候,要把QObject放在最前面,否则报错——C++认为人性本恶,默认都是私有的,这点和Delphi的世界观不一样
在买来的控件(没有源码)的基础上,想加入QObject的一些特性,不得不多继承: class MyProgress : public CProgress, public QObject 但总是报错: ...