How to change __autoload to spl_autoload_register using array?

Asked

Viewed 20 times

0

I know there’s a lot of websites about it. But I confess that I searched and did not understand.

The point is that this site is old and was on a server needed to change it to newer version of PHP.

And I came across this mistake:

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

I know it’s about __autoload being obsolete, but I’m not getting the basics of adding spl_autoload_register into this call.

<?php
function __autoload($className) {
    $classpath = array(
        'model/','helpers/','lib/','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;
        }
    }
}
?>

I tried to do so:

 <?php
    function autoload_1($className) {
        $classpath = array(
            'model/','helpers/','lib/','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;
            }
        }
    }
spl_autoload_register('autoload_1');

But this returning blank page without error, that’s where I got lost.

Someone could help or guide me to the solution.

Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.