1

I have a website that was created with php 5.6 and need to update to php8.0.10.

So, in this firts troble we have a function __autoload that is no longer supported.

How can a I change to spl_autoload_register?

Look my code:

function __autoload($className) {
    $classpath = array(
        'model/','helpers/','plugin/email/',//frontend
        '../model/','../helpers/','../plugin/email/'//backend
        );    
    $classFile = ucfirst($className) . ".php";
    $loaded = false;
    foreach ($classpath as $path) {
        if (is_readable("$path$classFile")) {
            require "$path$classFile";
            $loaded = true;
            break;
        }
    }
} 
Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • 2
    Is [php's own example](https://www.php.net/manual/en/function.spl-autoload-register.php?#example-3654) as replacement to `__autoload` not enough info? – Paul T. Nov 10 '21 at 02:12
  • Another [example](https://stackoverflow.com/questions/50780678/deprecated-autoload-is-deprecated-use-spl-autoload-register) and [example](https://stackoverflow.com/questions/10687804/how-to-use-spl-autoload-instead-of-autoload) – vee Nov 10 '21 at 02:40
  • https://getcomposer.org/ and generate a classmap. – Ярослав Рахматуллин Nov 10 '21 at 03:48

1 Answers1

0

Don't bother. Use composer and remove the old stuff.

https://getcomposer.org/doc/04-schema.md#classmap

e.g.

{
    "autoload": {
        "classmap": ["src/", "lib/", "Something.php"]
    }
}

when that's set up properly (after composer init and so on), just run this:

  • composer dump