框架编程之路一

一:自动加载为啥舍弃__autoload() 而选用 spl_register_autoload() ?

I experienced it while upgrading the Smarty and my own autoload function.



So, the better way is to avoid __autoload() and use spl_register_autoload() instead. If you have written this already, just rename your function to something like __autoload_my_classes, and in the next, call spl_autoload_register as:



<?php

function __autoload_my_classes($classname)

{

  # ... your logic to include classes here

}

spl_autoload_register('__autoload_my_classes');

?>

1:You can assign multiple functions to spl_autoload_register() by calling it repeatedly with different function names. Be sure to limit your every function to include a CLASS file.(确保每个自动加载函数只包含一个class文件)
 
注意:
1、如果使用 PHP 的 CLI 交互模式 时,Autoloading 不存在。
2、在 __ autoload 函数中抛出的异常不能被 catch 语句块捕获并导致致命错误。
 
 参考:
php自动加载: http://www.2cto.com/kf/201212/180553.html
PHP autoload与spl_autoload自动加载机制的深入理解: http://www.jb51.net/article/37906.htm
 
 

 

你可能感兴趣的:(编程)