今天更新了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();
希望能帮助遇到此问题的朋友,让大家少走弯路。