phpclosuressplspl-autoload-registerspl-autoloader

spl_autoload_register's different behaviour b/n online and localhost


enter image description here

I have the following directory structure, I put all my classes inside classes/ as shown below, and I have a header.php which both calls all files, and has other important settings that all files share.

Now, If I use spl_autoload_register() this way:

spl_autoload_register(function($class){
        include 'classes/'. $class .'.class.php';
    });

inside inc/header.php file and call this header.php from my index file, then it works fine on my localhost, but when I upload all the script as-is to a live host, then I get errors like.

Warning: include(classes/filehandler.class.php) [function.include]: failed to open stream: No such file or directory in /home/.../public_html/....com/inc/header.php on line 9 

Line 9 is the spl_autoload_register()

I don't get, how this could happen, and hoping anyone has any idea at all.

thanks


Solution

  • To show what i mean with my comments, add this code in your index.php file and remove the autoloader from your other files:

    spl_autoload_register(function($class){
        $classesPath = dirname(__FILE__) . '/classes/';
        if (is_file($classFile = $classesPath . $class.'.class.php')) {
            include $classFile;
        }
    });
    

    For inc/header.php:

    spl_autoload_register(function($class){
        $classesPath = dirname(__FILE__) . '/../classes/';
        if (is_file($classFile = $classesPath . $class.'.class.php')) {
            include $classFile;
        }
    });