一 預定義接口
1.1 遍歷
Traversable(遍歷)接口
檢測一個類是否可以被foreach遍歷,該接口不能被單獨的實現,只能由Iterator或IteratorAggregate實現。這是一個無法在 PHP 腳本中實現的內部引擎接口。IteratorAggregate或Iterator接口可以用來代替它。
接口摘要:
Traversable{
}
這個接口沒有任何方法,它的作用僅僅是作為所有可遍歷類的基本接口。
1.2 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 IteratorDemo implements Iterator, Countable
{
public $items = ['jason', 'jude', 'lisy'];
public $position;
public function __construct()
{
$this->position = 0;
}
// Countable接口的抽象方法
public function count()
{
return count($this->items);
}
//Iterator里定義的抽象的方法
public function current()
{
return $this->items[$this->position];
}
public function key()
{
return $this->position;
}
public function next()
{
++$this->position;
}
public function rewind()
{
$this->position = 0;
}
public function valid()
{
return isset($this->items[$this->position]);
}
}
spl 已為我們實現了常見的迭代器:
DirectoryIterator(extendsSplFileInfo)
1.3 IteratorAggregate(聚合式迭代器)接口
創建外部迭代器的接口
接口摘要:
IteratorAggregate?extends?Traversable{
/* 方法 */
abstract public Traversable?getIterator(void)
}
示例:
class IteratorAggregateDemo implements IteratorAggregate?
{
public $name = 'jason';
private $age = 18;
public $hobit = 'basketball';
//實現IteratorAggregate接口里的抽象方法
public function getIterator()
{
return new ArrayIterator($this);
}
}
1.4 ArrayAccess(數組式訪問)接口
提供像訪問數組一樣訪問對象的能力的接口
接口摘要
ArrayAccess{
/* 方法 */
abstract public boolean offsetExists(mixed $offset) // 檢查一個偏移位置是否存在,調用isset,empty時調用
abstract public mixed offsetGet(mixed $offset) // 獲取一個偏移位置的值
abstract public void offsetSet(mixed $offset,mixed $value) // 設置一個偏移位置的值
abstract public void offsetUnset(mixed $offset) //復位一個偏移位置的值
}
示例:
class ArrayAccessDemo implements ArrayAccess
{
public $name;
public? $age;
public $habit;
public function __construct($name, $age, $habit)
{
$this->name = $name;
$this->age = $age;
$this->habit = $habit;
}
public function offsetExists($offset)
{
if (array_key_exists($offset, get_object_vars($this))) {
return true;
} else {
return false;
}
}
public function offsetSet($offset, $value)
{
if ($this->offsetExists($offset)) {
$this->$offset = $value;
}
}
public function offsetGet($offset)
{
if ($this->offsetExists($offset)) {
return $this->$offset;
}
}
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->$offset);
}
}
}
1.5 序列化接口
序列化和反序列化一個對象,實現此接口的類將不再支持__sleep()和__wakeup()
接口摘要:
Serializable{
/* 方法 */
abstract public string?serialize(void) // serialize一個對象時調用
abstract public mixed?unserialize(string $serialized) //unserialize一個對象時調用
}
示例:
class Pot implements Serializable
{
protected $_a;
protected $_b;
public function serialize()
{
return serialize(get_object_vars($this));
}
public function unserialize($data)
{
$values = unserialize($data);
foreach ($values as $key=>$value) {
$this->$key = $value;
}
}
}
1.6 Closure 類
用于代表匿名函數的類
接口摘要:
Closure {
/* 方法 */
__construct ( void )? //用于禁止實例化的構造函數
public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) //復制一個閉包,綁定指定的$this對象和類作用域
public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] ) //復制當前閉包對象,綁定指定的$this對象和類作用域
}
示例:
class A{
function__construct($val) {
val = $val;
}
function getClosure() {
//returns closure bound to this object and scope
return function() { return $this->val; };
}
}
$ob1 = new A(1);
$ob2 = new A(2);
$cl = $ob1->getClosure();
echo $cl(), "\n";
$cl = $cl->bindTo($ob2);
echo $cl(), "\n";
?>
1.7 生成器類
生成器提供了一種更容易的方法來實現簡單的對象迭代,相比較定義類實現Iterator接口的方式,性能開銷和復雜性大大降低。生成器允許你在foreach代碼塊中寫代碼來迭代一組數據而不需要在內存中創建一個數組, 那會使你的內存達到上限,或者會占據可觀的處理時間。相反,你可以寫一個生成器函數,就像一個普通的自定義函數一樣, 和普通函數只返回一次不同的是, 生成器可以根據需要yield多次,以便生成需要迭代的值。
接口摘要:
Generator implements Iterator {
/* 方法 */
public mixed current ( void ) //返回當前產生的值
public mixed key ( void ) //返回當前產生的鍵
public void next ( void ) //生成器繼續執行
public void rewind ( void ) //重置迭代器
public mixed send ( mixed $value ) //向生成器中傳入一個值
public void throw ( Exception $exception ) //向生成器中拋入一個異常
public bool valid ( void ) //檢查迭代器是否被關閉
public void __wakeup ( void ) //序列化回調
}
對象不能通過new實例化.
示例:
functionfib($n)
{
$cur=1;
$prev=0;
for ($i=0;$i<$n;$i++) {
yield $cur;
$temp=$cur;
$cur=$prev+$cur;
$prev=$temp;
}
}
$fibs = fib(9); //函數調用,返回一個Generator對象
foreach ($fibs as $fib) {
echo " ".$fib;
generator對象被迭代時,會被多次的調用,每次調用會繼續執行yield后面的代碼,遇到yield返回yield指定的對象。
二 SPL 函數
class_implements— 返回指定的類實現的所有接口。
usage: array class_implements(mixed $class [,bool $autoload] )
說明:本函數返回一個數組,該數組中包含了指定類class及其父類所實現的所有接口的名稱。
參數:
$class: 對象(類實例)或字符串(類名稱)
$autoload: 是否允許使用__autoload魔術函數來自動裝載該類。默認值為TRUE
class_parents— 返回指定類的父類。
Usage: array class_parents ( mixed $class [, bool $autoload ] )
說明: 本函數返回一個包含了指定類class父類名稱的數組.
參數:
$class: 對象(類實例)或字符串(類名稱)
$autoload: 是否允許使用__autoload魔術函數來自動裝載該類。默認值為TRUE
class_uses— 返回一個類使用的trait
iterator_apply— 為迭代器中每個元素調用一個用戶自定義函數
Usage: int iterator_apply ( Traversable $iterator , callable $function [, array $args ] )
說明:
循環迭代每個元素時調用某一回調函數
參數說明:
$iterator 需要循環迭代的類對象
$function 循環迭代時調用的方法, (注意:如果沒有返回值或返回false,回調方法只會執行一次)
$args ?傳給回調方法的參數
iterator_count— 計算迭代器中元素的個數
Usage: int iterator_count ( Traversable $iterator )
iterator_to_array— 將迭代器中的元素拷貝到數組
Usage: array iterator_to_array ( Traversable $iterator [, bool $use_keys = true ] )
說明:
將迭代器中的元素拷貝到數組
參數說明:
$iterator 被拷貝的迭代器
$use_keys 是否使用迭代器中的key作為數組的key,默認true, (false 數字索引作為數組的key)
spl_autoload_call— 嘗試調用所有已注冊的__autoload()函數來裝載請求類
Usage: void spl_autoload_call ( string $class_name )
說明:
可以直接在程序中手動調用此函數來使用所有已注冊的__autoload函數裝載類或接口
參數說明:
$class_name 搜索的類名
spl_autoload_extensions— 注冊并返回spl_autoload函數使用的默認文件擴展名。
Usage: string spl_autoload_extensions ([ string $file_extensions ] )
說明:
本函數用來修改和檢查__autoload()函數內置的默認實現函數spl_autoload()所使用的擴展名
參數說明:
$file_extensions ?當不使用任何參數調用此函數時,它返回當前的文件擴展名的列表,不同的擴展名用逗號分隔。要修改文件擴展名列表,用一個逗號分隔的新的擴展名列表字符串來調用本函數即可。中文注:默認的spl_autoload函數使用的擴展名是".inc,.php"
spl_autoload_functions— 返回所有已注冊的__autoload()函數。
Usage: array spl_autoload_functions ( void )
說明:
獲取所有已注冊的 __autoload() 函數
返回說明:
包含所有已注冊的__autoload函數的數組(array)。如果自動裝載函數棧未激活,則返回FALSE。如果沒有已注冊的函數,則返回一個空數組
spl_autoload_register— 注冊給定的函數作為 __autoload 的實現
Usage: bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
說明:
將函數注冊到SPL ?__autoload函數隊列中。如果該隊列中的函數尚未激活,則激活它們。如果在你的程序中已經實現了__autoload()函數,它必須顯式注冊到__autoload函數隊列中。因為 spl_autoload_register()函數會將Zend Engine中的__autoload()函數取代為spl_autoload()或spl_autoload_call()。如果需要多條 autoload 函數,spl_autoload_register() 滿足了此類需求。 它實際上創建了 autoload 函數的隊列,按定義時的順序逐個執行。相比之下, __autoload() 只可以定義一次。
參數說明:
$autoload_function 欲注冊的自動裝載函數。如果沒有提供任何參數,則自動注冊 autoload 的默認實現函數spl_autoload()
$throw 此參數設置了autoload_function無法成功注冊時,spl_autoload_register()是否拋出異常
$prepend 如果是 true,spl_autoload_register()會添加函數到隊列之首,而不是隊列尾部.默認會添加到隊列之尾。
spl_autoload_unregister— 注銷已注冊的__autoload()函數
Usage: bool spl_autoload_unregister ( mixed $autoload_function )
說明:
從spl提供的自動裝載函數棧中注銷某一函數。如果該函數棧處于激活狀態,并且在給定函數注銷后該棧變為空,則該函數棧將會變為無效。如果該函數注銷后使得自動裝載函數棧無效,即使存在有__autoload函數它也不會自動激活
參數說明:
$autoload_function 需要注銷的自動加載函數
spl_autoload— __autoload()函數的默認實現
Usage: void spl_autoload ( string $class_name [, string $file_extensions ] )
說明:
本函數提供了__autoload()的一個默認實現。如果不使用任何參數調用 spl_autoload_register() 函數,則以后在進行 __autoload() 調用時會自動使用此函數。
參數說明:
$class_name 搜索的類名
$file_extensions ?自動加載的文件的擴展名
spl_classes— 返回所有可用的SPL類
spl_object_hash— 返回指定對象的hash id
Usage: string spl_object_hash ( object $obj )
說明:
本函數為指定對象返回一個唯一標識符。這個標識符可用于作為保存對象或區分不同對象的hash key。
參數說明:
$object?具體對象