smarty3.0和__autoload()冲突

来源:互联网 发布:重启mysql命令 编辑:程序博客网 时间:2024/05/22 06:23


今天更新了一下smarty到3.0,结果发现项目中的__autoload()不能用了,原来是因为smarty改变了autoload的方式。

解决方法如下:

function autoload($className){
    if (file_exists(SITEROOT.'include/module/'.$className.'.class.php'))
    include SITEROOT.'include/module/'.$className.'.class.php';
    else
    die('类'.$className.'文件不存在');
}
spl_autoload_register("autoload");

用上面的方式还可以依顺序同时载入几个autoload方法哦


来源: http://hi.baidu.com/aboc/blog/item/b9130ff4345ce978ddc474c3.html



另有解决办法如下

bimal at sanjaal dot com05-Jun-2011 06:58
When multiple scripts try to use __autoload(), there is a collision.
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');
?>

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.


from: http://cn2.php.net/manual/en/language.oop5.autoload.php#104282


原创粉丝点击