欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP SPL標(biāo)準(zhǔn)庫中的常用函數(shù)介紹

 更新時(shí)間:2015年05月11日 11:46:56   投稿:junjie  
這篇文章主要介紹了PHP SPL標(biāo)準(zhǔn)庫中的常用函數(shù)介紹,本文著重講解了spl_autoload_extensions()、spl_autoload_register()、spl_autoload()三個(gè)函數(shù),需要的朋友可以參考下

PHP SPL標(biāo)準(zhǔn)庫中提供了一些函數(shù)用來處理如自動(dòng)加載、迭代器處理等。

spl_autoload_extensions()添加spl_autoload()可加載的文件擴(kuò)展名
spl_autoload_register()注冊(cè)函數(shù)到SPL __autoload函數(shù)棧中。

復(fù)制代碼 代碼如下:

/*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會(huì)在這些path中去尋找類文件,可通過PATH_SEPARATOR添加多個(gè)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),它會(huì)去include_path中加載文件(.php/.inc)

復(fù)制代碼 代碼如下:

/*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注冊(cè)函數(shù)來加載文件

復(fù)制代碼 代碼如下:

/*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 — 為迭代器中每個(gè)元素調(diào)用一個(gè)用戶自定義函數(shù)
iterator_count — 計(jì)算迭代器中元素的個(gè)數(shù)
iterator_to_array — 將迭代器中的元素拷貝到數(shù)組
spl_autoload_functions — 返回所有已注冊(cè)的__autoload()函數(shù)
spl_autoload_unregister — 注銷已注冊(cè)的__autoload()函數(shù)
spl_classes — 返回所有可用的SPL類
spl_object_hash — 返回指定對(duì)象的hash id

如iterator相關(guān)函數(shù)使用:

復(fù)制代碼 代碼如下:

$iterator  = new  ArrayIterator (array( 'recipe' => 'pancakes' ,  'egg' ,  'milk' ,  'flour' ));
 
print_r(iterator_to_array($iterator)); //將迭代器元素轉(zhuǎn)化為數(shù)組
echo iterator_count($iterator); //計(jì)算迭代器元素的個(gè)數(shù)
print_r(iterator_apply($iterator, 'print_item', array($iterator)));//為迭代器每個(gè)元素調(diào)用自定義函數(shù)
 
 
function print_item(Iterator $iterator)
{
    echo  strtoupper ( $iterator -> current ()) .  "\n" ;
    return  TRUE ;
}

相關(guān)文章

最新評(píng)論