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 ...
随机推荐
- 改变VS2013的菜单栏字母为小写
REG ADD HKCU\Software\Microsoft\VisualStudio\12.0\General /v SuppressUppercaseConversion /t REG_DWOR ...
- ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered
正如以下msdn论坛所述,Microsoft.Jet.OLEDB.4.0只有32bit,所以我们必须compile application to x86 platform. http://social ...
- javascript 高级程序设计学习笔记(面向对象的程序设计)继承
ECMAScript中描述了原型链的概念,原型链是实现继承的主要方法. 实现原型链继承有一种基本模式 function SuperType () { this.property = true; } S ...
- [Tree]Count Complete Tree Nodes
Total Accepted: 22338 Total Submissions: 97234 Difficulty: Medium Given a complete binary tree, coun ...
- 自己动手打造html5星际迷航!
学习html5的canvas第三天,觉得还没过瘾,转眼就忘,于是趁着有空,准备弄个小游戏来玩!游戏应该需要注意性能,还有一些逻辑需要斟酌,我想还需要用户可修改性,也就是用户配置.好,开始我们简单但有趣 ...
- tcl/tk demo
环境及版本说明: OSX10.9 tclsh -> tclsh8.5 wish -> wish8.5 查看本机运行环境: which wish; 2 /usr/bin/wish which ...
- 痛苦的版本对齐(3) cygwin下的路径引用
[续<痛苦的版本对齐(2) 和时间的相关性>]http://www.cnblogs.com/yvivid/p/3541142.html 初步定位,如下告警为.depend文件路径问题导致. ...
- FormSheet式模态视图,点击模态视图外隐藏模态视图的方法
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- MySQL load data infile
语法: load data [low_priority] [local] infile ‘file_path' [replace] [ignore] into table table_name [(c ...
- 定制化Azure站点Java运行环境(2)
Azure Website上发布Java web应用 在Azure站点上发布Java Web应用非常简单,可以使用git从源代码发布,也可以使用FTP/FTPs直接发布,本节介绍FTP方式. 准备好你 ...