生活随笔
收集整理的這篇文章主要介紹了
详解spl_autoload_register()函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在了解這個函數之前先來看另一個函數:__autoload。??
一、__autoload??
這是一個自動加載函數,在PHP5中,當我們實例化一個未定義的類時,就會觸發此函數。看下面例子:??
| function?__autoload(?$class?) { |
| ?$file?=?$class?.?'.class.php';?? |
| ?if?(?is_file($file) ) {?? |
??
運行index.php后正常輸出hello world。在index.php中,由于沒有包含printit.class.php,在實例化printit時,自動調用__autoload函數,參數$class的值即為類名printit,此時printit.class.php就被引進來了。??
在面向對象中這種方法經常使用,可以避免書寫過多的引用文件,同時也使整個系統更加靈活。??
二、spl_autoload_register()??
再看spl_autoload_register(),這個函數與__autoload有與曲同工之妙,看個簡單的例子:??
| function?loadprint(?$class?) { |
| ?$file?=?$class?.?'.class.php';?? |
| spl_autoload_register(?'loadprint'?);? |
將__autoload換成loadprint函數。但是loadprint不會像__autoload自動觸發,這時spl_autoload_register()就起作用了,它告訴PHP碰到沒有定義的類就執行loadprint()。?
spl_autoload_register()?調用靜態方法?
| ?public?static?function?loadprint(?$class?) { |
| ??$file?=?$class?.?'.class.php';?? |
| ??if?(is_file($file)) {?? |
| ???require_once($file);?? |
| spl_autoload_register( ?array('test','loadprint')? ); |
| //另一種寫法:spl_autoload_register(??"test::loadprint"? );? |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
網上關于SPL spl_autoload_register的用法的例子有很多很多,自己也查看了很多,但感覺介紹得并不太詳細,使自己真正能明白其中的原理苦悶了好一會兒?,F將自己的理解記錄下來。
?
?????? 關于 Standard PHP Library (SPL) 的 autoload 的方法,這些都是 PHP 5.1.2 之后才加上的方法。為了方便,這里做了一些設定。假設你有類文件,放在/home/user/class/foo.class.php, 你當前的文件為/home/user/webroot/test.php, 示例代碼如下。
在文件test.php中:
1 <?
php
2
3 class autoload
4 {
5 public static function load(
$class name )
6 {
7 $filename = "/home/user/class/".
$classname."class.php"
;
8 if (
file_exists(
$filename )) {
9 require_once $filename ;
10 }
11 }
12 }
13
14 function __autoload(
$class name )
15 {
// 這個是默認的 autoload 方法
16 $filename = "/home/user/class/".
$classname."class.php"
;
17 if (
file_exists(
$filename )) {
18 require_once $filename ;
19 }
20 }
21
22 // 注冊一個 autoloader
23 spl_autoload_register( 'autoload::load'
);
24 /**
25 * __autoload 方法在 spl_autoload_register 后會失效,因為 autoload_func 函數指針已指向 spl_autoload 方法
26 * 可以通過下面的方法來把 _autoload 方法加入 autoload_functions list
27 */
28 spl_autoload_register( '__autoload'
);
29 // 注:下面的類看上去沒有定義,但其實系統根據sql_autoload_register提供的路徑會自動去/home/user// /class/*.class.php下搜索foo.class.php文件,如果沒找到才報錯。
30 $foo =
new foo();
31 $foo ->
bar();
32 ?>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
spl_autoload_register 是注冊php auto_load的函數,這個函數可以多次加載
? ? 每一個函數應該都有返回值(boolean),如果返回值為true則認為已經加載成功就退出了加載過程,如果失敗則繼續調用后邊的auto_load函數加載php文件,當然如果最后一個auto_load也沒有加載成功這時候就沒有加載完成,new的時候就會報錯。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php /** ?*?Created?by?PhpStorm. ?*?User:?chengtao ?*?Date:?14-7-3 ?*?Time:?上午11:27 ?*/ define('BASE_PATH',dirname(__FILE__).'/')?; function?cron_autoload1?($name)?{ ????$file?=?BASE_PATH.'lib1/'.$name.'.class.php'; ????if(file_exists($file)){ ????????include_once($file); ????????return?true; ????} } function?cron_autoload2?($name)?{ ????$file?=?BASE_PATH.'lib2/'.$name.'.class.php'; ????if(file_exists($file)){ ????????include_once($file); ????????return?true; ????} } spl_autoload_register('cron_autoload1'); spl_autoload_register('cron_autoload2'); new?Class1(); new?Class2(); |
總結
以上是生活随笔為你收集整理的详解spl_autoload_register()函数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。