Error: __autoload() is deprecated, use spl_autoload_register() instead

帅比萌擦擦* 提交于 2021-02-10 11:58:41

问题


I am facing a PHP error on live server. I believe it is a version issue.

The error is included below and occurs in the config.php file:-

ERROR: __autoload() is deprecated, use spl_autoload_register() instead.

Code snippet from config.php file

if (!function_exists('__autoload')) {
    function __autoload($class) {
        if (strpos($class, 'Auth_Controller') === 0) {
            @include_once( APPPATH . 'core/' . $class . EXT );
        }
        if (strpos($class, 'Rest_Controller') === 0) {
            @include_once( APPPATH . 'core/' . $class . EXT );
        }
    }
}

回答1:


Use spl_autoload_register to add a class loader function.

It is also a good practice to end the function just after finding the class.

$autoload = function ($class) {
    if (strpos($class, 'Auth_Controller') === 0) {
        @include_once( APPPATH . 'core/' . $class . EXT );
        return;
    }
    if (strpos($class, 'Rest_Controller') === 0) {
        @include_once( APPPATH . 'core/' . $class . EXT );
        return;
    }
};
spl_autoload_register($autoload);


来源:https://stackoverflow.com/questions/59045274/error-autoload-is-deprecated-use-spl-autoload-register-instead

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!