色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php自動(dòng)加載autoload機(jī)制示例分享

1,自定義函數(shù)

2,spl_autoload_register()

復(fù)制代碼 代碼如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*
-rw-rw-r-- 1 liuyuan liuyuan  800 Feb 19 11:39 ./func_autoload.php
-rw-rw-r-- 1 liuyuan liuyuan  906 Feb 19 11:28 ./spl_autoload.php

./include:
total 16
drwxrwxr-x 2 liuyuan liuyuan 4096 Feb 19 11:42 ./
drwxrwxr-x 3 liuyuan liuyuan 4096 Feb 19 11:43 ../
-rw-rw-r-- 1 liuyuan liuyuan  142 Feb 19 11:42 aClass.php
-rw-rw-r-- 1 liuyuan liuyuan  143 Feb 19 11:42 bClass.php

首先看自定義函數(shù)方式:

復(fù)制代碼 代碼如下:
<?php
    define('EOL', (php_SAPI == 'cli') ? php_EOL : '</ br>');
    print_r(get_included_files());
    echo EOL;
    print get_include_path();
    echo EOL;
    //set_include_path(get_include_path().PATH_SEPARATOR.'/var/www/ly_php/php_spl/include/');
    //set_include_path(dirname(__FILE__).'/include');
    //set_include_path(dirname(__FILE__).'/include/');

    function __autoload($className){
        $filename = './include/'.$className.'.php';
        //$filename = './include/'.$className.'.php';
        //$filename = '/var/www/ly_php/php_spl/include/'.$className.'.php';
        if(file_exists($filename)){
            include_once $filename;
        }else{
            exit('no file');
        }
    }

    $a = new aClass();
    $b = new bClass();
    print_r(get_included_files());
?>

運(yùn)行結(jié)果如下:

復(fù)制代碼 代碼如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php func_autoload.php
Array
(
    [0] => /var/www/phpgcs/php_autoload/func_autoload.php
)

.:/usr/share/php:/usr/share/pear
aClass is loaded
bClass is loaded
Array
(
    [0] => /var/www/phpgcs/php_autoload/func_autoload.php
    [1] => /var/www/phpgcs/php_autoload/include/aClass.php
    [2] => /var/www/phpgcs/php_autoload/include/bClass.php
)

第二種方式:

復(fù)制代碼 代碼如下:
<?php
    class myLoader{
        public static function autoload($className){
            $filename = './include/'.$className.'.php';
            if(file_exists($filename)){
                include_once $filename;
            }else{
                exit('no file');
            }
        }
    }

    define('EOL', (php_SAPI == 'cli') ? php_EOL : '<br />');

    spl_autoload_register(array('myLoader', 'autoload'));

    /**
    *__autoload 方法在 spl_autoload_register 后會(huì)失效,因?yàn)?autoload_func 函數(shù)指針已指向 spl_autoload 方法
    * 可以通過(guò)下面的方法來(lái)把 _autoload 方法加入 autoload_functions list
    */
    //spl_autoload_register( '__autoload' );

 
    error_reporting(E_ALL^E_NOTICE^E_WARNING^E_ERROR);
    error_reporting(E_NOTICE | E_WARNING );

    $a = new aClass();
    print_r(get_included_files());
    echo EOL;
    $b = new bClass();
    echo EOL;
?>

運(yùn)行結(jié)果如下:

復(fù)制代碼 代碼如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php spl_autoload.php
aClass is loaded
Array
(
    [0] => /var/www/phpgcs/php_autoload/spl_autoload.php
    [1] => /var/www/phpgcs/php_autoload/include/aClass.php
)

bClass is loaded

php技術(shù)php自動(dòng)加載autoload機(jī)制示例分享,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 南开区| 罗定市| 公安县| 英山县| 平顶山市| 乌拉特中旗| 乐陵市| 德令哈市| 新乡市| 鲁甸县| 永顺县| 惠州市| 仙桃市| 镇江市| 甘洛县| 潮州市| 屏东市| 郁南县| 托里县| 华容县| 寿阳县| 济阳县| 昌吉市| 华坪县| 陕西省| 修水县| 石台县| 长垣县| 永春县| 乳山市| 林州市| 扬州市| 绥中县| 涞源县| 轮台县| 连云港市| 阿尔山市| 秀山| 随州市| 体育| 英超|