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 ...
随机推荐
- 详解AJAX核心 —— XMLHttpRequest 对象 (上)
我要说的内容都是非常基础的内容,高手就免看了,如果看了欢迎给点意见啊.新手或者对低层还不是很了解的人可以看看,帮助理解与记忆. XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程 ...
- MVC 笔记(二)
HttpUtility.HtmlEncode来预处理用户输入,这能阻止用户向视图中用链接注入js代码或html标记 .[Required]:非空验证 .[StringLength(**)]:设置字符的 ...
- ios9基础知识总结(一)
I--load 类被加载时自动调用,只要类的项目中,运行时就会加载.类一加载,此方法就会调用 //类被加载时调用,只要类的项目中,运行时就会加载,类一加载,此方法就调用 + (void)load { ...
- R - 递推
Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平 ...
- linux网络编程:使用单进程实现多客户端通信
服务端: //回射服务器 //避免僵尸进程 #include "unistd.h" #include "sys/types.h" #include " ...
- 解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
因为最近更新的PHP版本,写sql语句,忽然发现不能用了,上网查了一些原因,找到几个方法如下: 1.禁止php报错 display_errors = on 改成 display_errors = of ...
- 初识scrapy,美空网图片爬取实战
这俩天研究了下scrapy爬虫框架,遂准备写个爬虫练练手.平时做的较多的事情是浏览图片,对,没错,就是那种艺术照,我骄傲的认为,多看美照一定能提高审美,并且成为一个优雅的程序员.O(∩_∩ ...
- Pie--hdu1969(二分法)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- Train Problem II
问题陈述: HDOJ Problem - 1023 问题解析: 卡特兰数(Catalan)的应用 基本性质: f(n) = f(1)f(n-1) + f(2)f(n-2) + ... + f(n-2) ...
- Git学习01 --git add, git commit , git log ,git status, git reset --hard, head
Git官方提供的快速入门教程:https://try.github.io/levels/1/challenges/1 特点:Git极其强大的分支管理:分布式版本 集中式版本控制系统,版本库是集中存放在 ...