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 ...
随机推荐
- iOS 获取系统目录
//获取根目录 NSString *homePath = NSHomeDirectory(); NSLog(@"Home目录:%@",homePath); //获取Document ...
- C# Winform对文件夹的权限判断及处理
WindowsIdentity类可以获取当前执行者的身份信息 /// <summary> /// 递归搜索文件方法 /// </summary> /// <param n ...
- codeforces #332 div2
A. #include<cstdio> #include<algorithm> #include<cmath> #include<map> #inclu ...
- E - The King
Description Once upon a time in a country far away lived a king and he had a big kingdom. He was a v ...
- linux网络编程:三次握手与四次挥手
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 其中三次握手即建立连接 四次挥手则为关闭连接 TCP连接的11种状态 客户端独有的:(1)SYN_SENT (2)FIN ...
- JSON之三:获取JSON文本并解释(以google的天气API为例)
google提供了天气的api,以广州天气为例,地址为: http://api.openweathermap.org/data/2.5/weather?q=guangzhou 返回的结果为: { ...
- Android系统信息
前提:获取的都是AndroidMainfest.xml下的信息 一.PackageManager 负责管理所有已安装的App 二.ActivityInfo 封装了Mainifest中的<acti ...
- Leetcode算法刷题:第100题 Same Tree
Same Tree 题目 给予两棵二叉树,判断这两棵树是否相等(即各节点的值都一样) 解题思路 分别遍历两棵二叉树,并用列表分别存储这两棵树的节点的值,比较这两个列表就可以了 class Soluti ...
- API接口测试01理论
定义 接口测试是系统组件间及多个系统之间的测试 如:app与Server间如何进行数据交换,传递 意义 确保主要流程及系统的稳定性 将BUG控制在项目前期 缩短产品的研发周期 检查服务器的异常处理能力 ...
- EasyUI 使用心得
最近项目中用到EasyUI,总结了一下 注:EasyUI中所有的控件不能重名,否则会出现意向不到的后果.这是EasyUI框架决定的. ① EasyUI 获取文本框中的值 //日期 $('#beginD ...