PHP SPL標(biāo)準(zhǔn)庫中的常用函數(shù)介紹
PHP SPL標(biāo)準(zhǔn)庫中提供了一些函數(shù)用來處理如自動加載、迭代器處理等。
spl_autoload_extensions()添加spl_autoload()可加載的文件擴(kuò)展名
spl_autoload_register()注冊函數(shù)到SPL __autoload函數(shù)棧中。
/*test1.php*/
<?php
class Test1
{
}
/*test2.lib.php*/
<?php
class Test2
{
}
/*test.php*/
<?php
//設(shè)置可加載類的文件擴(kuò)展名
spl_autoload_extensions(".php,.inc.php,.class.php,.lib.php");
//設(shè)置include_path,autoload會在這些path中去尋找類文件,可通過PATH_SEPARATOR添加多個path
set_include_path(get_include_path().PATH_SEPARATOR.'libs/');
//不提供參數(shù),默認(rèn)實(shí)現(xiàn)函數(shù)是spl_autoload()
spl_autoload_register();
$test1 = new Test1();
$test2 = new Test2();
spl_autoload()它是__autoload()的默認(rèn)實(shí)現(xiàn),它會去include_path中加載文件(.php/.inc)
/*test1.php*/
<?php
class Test1
{
}
/*test.php*/
<?php
set_include_path(get_include_path().PATH_SEPARATOR.'libs/');
spl_autoload('test1');
$test1 = new Test1();
spl_autoload_call()調(diào)用所有spl_autoload_register注冊函數(shù)來加載文件
/*test1.php*/
<?php
class Test
{
public function getFilename()
{
echo 'test1.php';
}
}
/*test2.lib.php*/
<?php
class Test
{
public function getFilename()
{
echo 'test2.lib.php';
}
}
/*test.php*/
<?php
function loader($classname)
{
if($classname == 'Test1') {
require __DIR__ . '/test1.php';
}
if($classname == 'Test2') {
require __DIR__ . '/test2.lib.php';
}
}
spl_autoload_register('loader');
spl_autoload_call('Test2');
$test = new Test();
$test->getFilename(); //test2.lib.php
其它SPL 函數(shù)介紹:
class_implements — 返回指定的類實(shí)現(xiàn)的所有接口。
class_parents — 返回指定類的父類。
class_uses — Return the traits used by the given class
iterator_apply — 為迭代器中每個元素調(diào)用一個用戶自定義函數(shù)
iterator_count — 計(jì)算迭代器中元素的個數(shù)
iterator_to_array — 將迭代器中的元素拷貝到數(shù)組
spl_autoload_functions — 返回所有已注冊的__autoload()函數(shù)
spl_autoload_unregister — 注銷已注冊的__autoload()函數(shù)
spl_classes — 返回所有可用的SPL類
spl_object_hash — 返回指定對象的hash id
如iterator相關(guān)函數(shù)使用:
$iterator = new ArrayIterator (array( 'recipe' => 'pancakes' , 'egg' , 'milk' , 'flour' ));
print_r(iterator_to_array($iterator)); //將迭代器元素轉(zhuǎn)化為數(shù)組
echo iterator_count($iterator); //計(jì)算迭代器元素的個數(shù)
print_r(iterator_apply($iterator, 'print_item', array($iterator)));//為迭代器每個元素調(diào)用自定義函數(shù)
function print_item(Iterator $iterator)
{
echo strtoupper ( $iterator -> current ()) . "\n" ;
return TRUE ;
}
- PHP SPL標(biāo)準(zhǔn)庫之文件操作(SplFileInfo和SplFileObject)實(shí)例
- PHP SPL標(biāo)準(zhǔn)庫之?dāng)?shù)據(jù)結(jié)構(gòu)棧(SplStack)介紹
- PHP SPL標(biāo)準(zhǔn)庫之?dāng)?shù)據(jù)結(jié)構(gòu)堆(SplHeap)簡單使用實(shí)例
- 解析PHP SPL標(biāo)準(zhǔn)庫的用法(遍歷目錄,查找固定條件的文件)
- PHP SPL標(biāo)準(zhǔn)庫之SplFixedArray使用實(shí)例
- PHP標(biāo)準(zhǔn)庫(PHP SPL)詳解
- PHP SPL標(biāo)準(zhǔn)庫之接口(Interface)詳解
- PHP使用標(biāo)準(zhǔn)庫spl實(shí)現(xiàn)的觀察者模式示例
- PHP標(biāo)準(zhǔn)庫 (SPL)——Countable用法示例
相關(guān)文章
PHP中的str_repeat函數(shù)在JavaScript中的實(shí)現(xiàn)
PHP中有一個函數(shù):String str_repeat($str, num);挺好用的,在 本文為大家介紹下次函數(shù)在js中的實(shí)現(xiàn),感興趣的朋友可以參考下2013-09-09
PHP簡單驗(yàn)證碼功能機(jī)制實(shí)例詳解
這篇文章主要介紹了PHP簡單驗(yàn)證碼功能機(jī)制,結(jié)合實(shí)例形式深入淺出的分析了php驗(yàn)證碼的原理、實(shí)現(xiàn)步驟及相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
PHP查找數(shù)組中只出現(xiàn)一次的數(shù)字實(shí)現(xiàn)方法【查找特定元素】
這篇文章主要介紹了PHP查找數(shù)組中只出現(xiàn)一次的數(shù)字實(shí)現(xiàn)方法,涉及php使用array_count_values針對數(shù)組元素進(jìn)行統(tǒng)計(jì)的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
php+ajax實(shí)現(xiàn)無刷新數(shù)據(jù)分頁的辦法
這篇文章主要介紹了php+ajax實(shí)現(xiàn)無刷新分頁的方法,詳細(xì)講述了數(shù)據(jù)庫的創(chuàng)建、Ajax文件的實(shí)現(xiàn)及PHP調(diào)用方法,需要的朋友可以參考下2015-11-11

