PHP SPL库
SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的组件和接口,并且从 PHP5.3 已逐渐的成熟。SPL 其实在所有的 PHP5 开发环境中被内置,同时无需任何设置。
一.spl_autoload_register
更加方便的惰性加载
二.Iterator:迭代器
迭代器是比较重要的一种设计模式.普遍用于数组遍历.php的Iterator接口,预定义了以下方法:
Iterator extends Traversable {
/* 方法 */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public boolean valid ( void )
}
任何类只要实现了该接口,则该类就可以迭代:
示例代码:
class Users implements Iterator
{
protected $position = 0;
protected $storage = array(); public function __construct()
{
$this->getWithData();
} public function getWithData()
{
$this->storage[] = array(
array('id' => 1, 'name' => 'john'),
array('id' => 2, 'name' => 'jake')
);
} public function current()
{
return $this->storage[$this->position];
} public function next()
{
$this->position ++;
} public function key()
{
return $this->position;
} public function valid()
{
return isset($this->storage[$this->position]);
} public function rewind()
{
$this->position = 0;
} } $users = new Users();
foreach ($users as $key => $value) {
echo $key;
var_dump($value);
} $users->rewind(); // 或者
while ($users->valid()) {
$cur = $users->current();
$users->next();
var_dump($cur);
}
SPL内置的迭代器类,可以方便的实现一些功能,比如迭代一个文件夹:
RecursiveIterator
RecursiveIteratorIterator
OuterIterator
IteratorIterator
FilterIterator
RecursiveFilterIterator
ParentIterator
SeekableIterator
LimitIterator
GlobIterator
CachingIterator
RecursiveCachingIterator
NoRewindIterator
AppendIterator
RecursiveIteratorIterator
InfiniteIterator
RegexIterator
RecursiveRegexIterator
EmptyIterator
RecursiveTreeIterator
ArrayIterator
ps:
注意区分ArrayIterator和ArrayAccess的区别;
ArrayAccess:提供像访问数组一样访问对象的能力的接口
三.ArrayAccess接口:
ArrayAccess {
/* 方法 */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}
示例代码(结合单例模式):
class Config implements ArrayAccess
{
private static $instance = null;
private $configs; private function __construct()
{
$this->configs = array(
'db_type' => 'mysql',
'db_user' => 'root'
);
} public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new Config();
}
return self::$instance;
} public function offsetExists($offset)
{
return isset($this->configs[$offset]);
} public function offsetGet($offset)
{
return $this->configs[$offset];
} public function offsetSet($offset, $value)
{
$this->configs[$offset] = $value;
} public function offsetUnset($offset)
{
unset($this->configs[$offset]);
} public function __toString()
{
return (string)var_export($this->configs, true);
} private function __clone()
{
}
} $config = Config::getInstance();
print $config['db_type'];
print "\n";
$config['db_pwd'] = '1123';
print $config;
四.SplFixedArray
SplFixedArray 实例化一个固定长度的数组.SplFixedArray数组相比标准的PHP数组更接近于C语言的数组,而且由于splFixedArray没有使用散列(Hash)存储方式,因此效率更高.
PHP SPL库的更多相关文章
- 在PHP中使用SPL库中的对象方法进行XML与数组的转换
虽说现在很多的服务提供商都会提供 JSON 接口供我们使用,但是,还是有不少的服务依然必须使用 XML 作为接口格式,这就需要我们来对 XML 格式的数据进行解析转换.而 PHP 中并没有像 json ...
- php spl库的使用(PHP标准库)【摘抄引用】
文章来源与推荐阅读:阮一峰--PHP SPL笔记 && PHP SPL使用方法和他的威力 1.SPL 是什么? SPL:standard php library php标准库,此 ...
- php—Spl库常用数据结构基本用法
数据结构之一 : 栈 //zhan $stack = new SplStack(); $stack->push('data1'); $stack->push('data2'); echo ...
- PHP 设计模式 笔记与总结(3)SPL 标准库
SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...
- 深入浅出 PHP SPL(PHP 标准库)(转)
一.什么是spl库? SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php ...
- PHP的SPL扩展库(一)数据结构
SPL 库也叫做 PHP 标准库,主要就是用于解决典型问题的一组接口或类的集合.这些典型问题包括什么呢?比如我们今天要讲的数据结构,还有一些设计模式的实现,就像我们之前讲过的观察者模式相关的接口在 S ...
- Arduino库和STM32的寄存器、标准库、HAL库、LL库开发比较之GPIO
标题: Arduino库和STM32的寄存器.标准库.HAL库.LL库开发比较之GPIO 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#Arduino,#STM32,#库,#开发 ...
- (转)yii流程,入口文件下的准备工作
yii流程 一 目录文件 |-framework 框架核心库 |--base 底层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集, ...
- PHP全栈工程师学习大纲
一.高性能网站开发功力提升 时间 标题 内容概要 2015-12-28 开学典礼以及工程师成长路线图 工程师成长的发展路径图.三个阶段,在各个阶段需要提升自己的地方,从技术上也讲了一些提高分析代码的工 ...
随机推荐
- codeforce 570 problem E&& 51Nod-1503-猪和回文
1503 猪和回文 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表 ...
- glEnable(GL_DEPTH_TEST)的问题
http://www.gameres.com/msg_195903.html 在程序中加入glEnable(GL_DEPTH_TEST)之后显示就完全黑屏了,即使是清空了深度缓冲glClear(GL_ ...
- python--线程池(concurrent.futures)
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat # 为什么需要线程池 # 1.主线程中可以获取某一个线程的状态或者某一个任 ...
- python每日一类(2):platform
根据官方文档的解释(https://docs.python.org/3.5/library/platform.html#module-platform): 学习其他人的代码如下: # python p ...
- [BZOJ2738]矩阵乘法 整体二分+二维树状数组
2738: 矩阵乘法 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1643 Solved: 715[Submit][Status][Discuss ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- java数组集合
一.ArrayList 1. ArrayList底层采用数组实现,当使用不带参数的构造方法生成ArrayList对象时,实际上会在底层生成一个长度为10的Object类型数组2. 如果增加的元素个数超 ...
- Codeforces 1027F. Session in BSU
题目直通车:Codeforces 1027F. Session in BSU 思路: 对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当 ...
- Codeforces Round #325 (Div. 2) Laurenty and Shop 模拟
原题链接:http://codeforces.com/contest/586/problem/B 题意: 大概就是给你一个两行的路,让你寻找一个来回的最短路,并且不能走重复的路. 题解: 就枚举上下选 ...
- 洛谷五月月赛 T1
做一下差分之后,把每个位置的差分数看成这个位置有多少个石子,于是每次操作就是选一个有石子的位置并把这个位置的石子移到后面的位置(如果这个位置已经是最后了那么直接扔掉). 所以就是带权石子问题了,最后判 ...