今天更新了smarty到3.0,結果發現項目中的__autoload()不能用了,
作為一個剛學習PHP 的菜鳥新手,搞了半天才明白問題出在它倆的沖突上,郁悶了好幾天。後通過查看,Smarty3.0 中的SMARTY2_BC_NOTES文件,得 知Smarty3.0跟php的__autoload()有沖突:
——————————————————————————-——-
== Autoloader ==
Smarty 3 does register its own autoloader with spl_autoload_register. If your code has
an existing __autoload function then this function must be explicitly registered on
the __autoload stack. See http://us3.php.net/manual/en/function.spl-autoload-register.php
for further details.
——————————— —————————————————————
解決辦法是使用spl_autoload_register()注冊自己的加載類。
現在給出配置前後的代碼:
————————————————————————————————————
if(substr($className, -6)=="Action"){ include(APP_CLASS_PATH.'Action/'.$className.".class.php"); }elseif(substr($className, -5)=="Model"){ include(APP_CLASS_PATH.'Model/'.$className.".class.php"); }elseif($className=="Smarty"){ require_once NET_ROOT."Smarty/Smarty.class.php"; }elseif(substr($className, -6)=="Public"){ include(APP_CLASS_PATH.'Public/'.$className.".class.php"); } —————————————————————————————— class ClassAutoloader { public function __construct() { spl_autoload_register(array($this, 'loader')); } private function loader($className) { if(substr($className, -6)=="Action"){ echo '1'; include(APP_CLASS_PATH.'Action/'.$className.".class.php"); }elseif(substr($className, -5)=="Model"){ echo '2'; include(APP_CLASS_PATH.'Model/'.$className.".class.php"); }elseif($className=="Smarty"){ echo '3'; require_once NET_ROOT."Smarty/Smarty.class.php"; }elseif(substr($className, -6)=="Public"){ echo '4'; include(APP_CLASS_PATH.'Public/'.$className.".class.php"); } } } $autoloader = new ClassAutoloader();
希望能幫助遇到此問題的朋友,讓大家少走彎路。