已弃用:__autoload()已弃用,请使用spl_autoload_register()

3
function __autoload($class) {
    if (strpos($class, 'CI_') !== 0) {
        if (file_exists($file = APPPATH . 'core/' . $class . php)) {
            include $file;
        }
    }
}

已弃用:__autoload() 已经被弃用,请使用spl_autoload_register() 和致命错误:在codeigniter中无法重新声明__autoload()错误


我不明白这个问题。这已经不再使用了,请改用这个方法听起来很清楚。停止使用__autoload()并使用spl_autoload_register()。有什么更清晰的陈述方式吗? - Ken White
首先,您使用的是哪个版本的CI? - user4419336
1
__autoload 已经在 PHP 7.2.0 中被弃用。依赖此功能是极不鼓励的。请参阅 http://php.net/manual/en/function.autoload.php。 - Pradeep
当使用sql_autoload_register()时,出现错误“致命错误:找不到类'Home_My_SmartPriceController'”。 - jadeja jaypalsinh
ED先生,我使用CodeIgniter 3。 - jadeja jaypalsinh
2
这个回答解决了你的问题吗?如何使用spl_autoload()代替__autoload() - Nico Haase
2个回答

6

如果要添加spl_autoload_register()函数,只需在routes.php中用您的代码替换此代码即可。

function my_autoloader($class) {
if (strpos($class, 'CI_') !== 0) {
    if (file_exists($file = APPPATH . 'core/' . $class . 'php')) {
        include $file;
    }
  }
}
spl_autoload_register('my_autoloader');

希望这能帮到您。

1
我将旧代码注释掉,并添加了@Jagadish的代码,这消除了我的日志中非常烦人的警告。 - rolinger

2
我希望这个对你有用,因为我正在使用它。
function your_autoloader($class) {

if (strpos($class, 'CI_') !== 0) {

    if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {

        include $file;

    }
  }
}
spl_autoload_register('your_autoloader');

如果这没有帮助到您,请引起我的注意。

你好,你还在吗?我仍然遇到同样的问题。 - Sandhya Srishti
1
@SandhyaSrishti,你能否把错误报告和一些调用代码发送给我? - Chibueze Agwu
非常感谢您的关注。我已经解决了那个问题。 - Sandhya Srishti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接