3

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 Answers1

3

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);
David Lemon
  • 1,560
  • 10
  • 21