中央論壇 - CENTER BBS

標題: [PHP] 函數spl_autoload_register() & __autoload()介紹及用法實作 [打印本頁]

作者: 郭子錢    時間: 2013-9-24 09:56
標題: [PHP] 函數spl_autoload_register() & __autoload()介紹及用法實作
1.__autoload() —— 自動加載函數

我把很多個類文件以類名.class.php的形式存放在class目錄中,在class的同級目錄中建立一個index.php。進入class目錄裡面分別建立class1.class.php、class2.class.php、class3.class.php文件,分別為裡面的文件添加一下代碼
  1. <?php
  2. //class1.class.php中
  3. class class1{
  4.         public function __construct(){
  5.                 echo "class1";
  6.         }
  7. }
  8. ?>

  9. <?php
  10. //class2.class.php中
  11. class class2{
  12.         public function __construct(){
  13.                 echo "class2";
  14.         }
  15. }
  16. ?>

  17. <?php
  18. //class3.class.php中
  19. class class3{
  20.         public function __construct(){
  21.                 echo "class3";
  22.         }
  23. }
  24. ?>
複製代碼
index.php文件中寫入
  1. <?php
  2. function __autoload($classname){
  3.         $filename = "./class/".$classname.".class.php";
  4.         if(is_file($filename)){
  5.                 include $filename;
  6.         }
  7. }

  8. $test1 = new class1();
  9. echo '<br/>';
  10. $test1 = new class2();
  11. echo '<br/>';
  12. $test1 = new class3();

  13. //结果是
  14. class1
  15. class2
  16. class3

  17. ?>
複製代碼
我們成功的自動加載了class下面所有的要加載的類。



2.spl_autoload_register() —— 註冊__autoload()函數

class裡面的文件不做任何改變,只是簡單的改寫一下index.php
  1. <?php
  2. // 写一个loadclass函数
  3. // loadclass函数不具备自动加载类的功能
  4. function loadclass($classname){
  5.         $filename = "./class/".$classname.".class.php";
  6.         if(is_file($filename)){
  7.                 include $filename;
  8.         }
  9. }
  10. // spl_autoload_register()函数让这个loadclass具备了自动加载类的功能
  11. spl_autoload_register("loadclass");

  12. $test1 = new class1();
  13. echo '<br/>';
  14. $test1 = new class2();
  15. echo '<br/>';
  16. $test1 = new class3();
  17. ?>
複製代碼
至於性能上面,發現很多框架裡面都是用的spl_autoload_register(),參考文獻說法是spl_autoload_register()較新且效能較好。



spl_autoload_register() 用法實驗過程:

第一步,使用spl_autoload_register()函數注冊load()方法
代碼如下:
  1. <?php
  2. function load(){
  3. require_once 'lib.php';
  4. }
  5. spl_autoload_register('load');
  6. ?>
複製代碼
其中lib.php文件代碼如下
代碼如下:
  1. <?php
  2. class className{
  3. function method(){
  4. echo 'a method in class';
  5. }
  6. }

  7. function onlyMethod(){
  8. echo 'method only';
  9. }
  10. ?>
複製代碼
說明:lib.php文件为一個className類和一個onlyMethod函數

第二步,調用自動加載類
代碼如下:
  1. $class = new className();
  2. $class->method();
  3. onlyMethod();
複製代碼
輸出:
a method in class
method only

說明:實例化className類,並調用類method()函數,同時調用onlyMethod()方法,輸出正常,沒有出現錯誤

第三步,直接調用函數
  1. onlyMethod();
複製代碼
說明:沒有實例化類,直接調用lib.php文件中的onlyMethod()函數
輸出:
Fatal error: Call to undefined function onlyMethod() in '...(省略路徑)'

第四步,實例化className類,再直接調用
  1. $class = new className();
  2. onlyMethod();
複製代碼
輸出:method only

從上面的四步實驗發現,如果加載的文件包含函數,使用則一定需要實例化�堶悸疑�,否則就產生異常情況 Call to undefined function錯誤,具體在使用中要注意一下。




歡迎光臨 中央論壇 - CENTER BBS (https://centerbbs.com/) Powered by Discuz! X3