php自動(dòng)加載autoload機(jī)制示例分享
1,自定義函數(shù)
2,spl_autoload_register()
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ù)方式:
<?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é)果如下:
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
)
第二種方式:
<?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 方法
* 可以通過下面的方法來把 _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é)果如下:
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
相關(guān)文章
PHP實(shí)現(xiàn)自動(dòng)發(fā)送郵件功能代碼(qq 郵箱)
本文給大家分享以qq郵箱為例給大家介紹PHP實(shí)現(xiàn)自動(dòng)發(fā)送郵件功能代碼,感興趣的朋友參考下吧2017-08-08在laravel中實(shí)現(xiàn)事務(wù)回滾的方法
今天小編就為大家分享一篇在laravel中實(shí)現(xiàn)事務(wù)回滾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10thinkphp5框架結(jié)合mysql實(shí)現(xiàn)微信登錄和自定義分享鏈接與圖文功能示例
這篇文章主要介紹了thinkphp5框架結(jié)合mysql實(shí)現(xiàn)微信登錄和自定義分享鏈接與圖文功能,涉及基于curl的微信接口調(diào)用、數(shù)據(jù)庫操作等相關(guān)使用技巧,需要的朋友可以參考下2019-08-08CI框架常用經(jīng)典操作類總結(jié)(路由,偽靜態(tài),分頁,session,驗(yàn)證碼等)
這篇文章主要介紹了CI框架常用經(jīng)典操作類,結(jié)合實(shí)例形式總結(jié)分析了CI框架URL、路由、偽靜態(tài)、分頁、session、驗(yàn)證碼等相關(guān)操作類與使用技巧,需要的朋友可以參考下2016-11-11PHP實(shí)現(xiàn)字母數(shù)字混合驗(yàn)證碼功能
PHP實(shí)現(xiàn)字母數(shù)字混合驗(yàn)證碼,支持自定義驗(yàn)證碼、驗(yàn)證碼圖片、寬度、高度、個(gè)數(shù)、背景圖片,本文給大家分享實(shí)例代碼,感興趣的朋友跟隨小編一起看看吧2019-07-07php的sprintf函數(shù)的用法 控制浮點(diǎn)數(shù)格式
這篇文章主要介紹了php的sprintf函數(shù)的用法,需要的朋友可以參考下2014-02-02