sql_autoload_register() 函數 和__autoload() 的區別


1:__autoload($class) 因為是一個函數,所以只能定義一次,使用多個會沖突報錯;而 sql_autoload_register('function') 可定義多個,它有效地創建一個隊列的自動裝載函數並按順序依次定義

2:SPL函數很豐富,有更多的操作空間:如spl_autoload_unregister()注銷已經注冊的函數、spl_autoload_functions()返回所有已經注冊的函數等

3: 自動加載對象更加方便,很多框架都是這樣做的:

 1 class ClassAutoloader {
 2     public function __construct() {
 3         spl_autoload_register(array($this, 'loader'));
 4     }
 5     private function loader($className) {
 6         echo 'Trying to load ', $className, ' via ', __METHOD__, "()\n";
 7         include $className . '.php';
 8     }
 9 }
10 
11 $autoloader = new ClassAutoloader();
12 
13 $obj = new Class1();
14 $obj = new Class2();

總結:推薦使用sql_autoload_register()函數,拋棄__autoload()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM