1

I've got an autoloader function in my application:

 spl_autoload_register(function ($className) {
if (file_exists(ROOT . DS . 'library'  . DS . 'intranet'  . DS  . 'classes' . DS .strtolower($className) . '.php')){
    require_once(ROOT . DS . 'library'  . DS . 'intranet'  . DS  . 'classes' . DS .strtolower($className) . '.php');
    print_r($className." loaded <br/>");
} else if (file_exists(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php')) {
    require_once(ROOT . DS . 'application' . DS . 'controller' . DS . strtolower($className) . '.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php')) {
    require_once(ROOT . DS . 'application' . DS . 'model' . DS . strtolower($className) . '.php');
} else if (file_exists(ROOT . DS . 'application' . DS . 'view' . DS . strtolower($className) . '.php')) {
    require_once(ROOT . DS . 'application' . DS . 'view' . DS . strtolower($className) . '.php');
} else {
    throw new Exception("Class: $className not autoloaded!");
}
});

In one of my files, a session manager, it calls a class called database. The database class has not been called before, and should then at that point be autoloaded.

However, it appears to be not - that said, I'm printing out a list of classes which are being loaded:

debug loaded 
sessionmanager loaded 
database loaded 
database loaded 

So it looks like it's being loaded, right?

In the sessionnmanager class:

Fatal error: Class 'database' not found 

Does my autoloader look fine?

I've put a print in the database class in the __construct method, yep, sure enough, it is being called.

8
  • Isit possible you have two database files and because of the conflict the autoloader is loading the wrong file one?
    – Iznogood
    Nov 18, 2012 at 20:10
  • There shouldn't be, but I've looked and there's only one in the project.
    – bear
    Nov 18, 2012 at 20:14
  • Did you check your library/intranet/classes/database.php file? Does it contain declaration of class "database"? Seems that not. Nov 18, 2012 at 20:29
  • @ElenaSharovar the application is working perfectly fine locally. /library/intranet/classes/database.php exists and only contains one declaration
    – bear
    Nov 18, 2012 at 20:31
  • 1
    You should never trow exceptions in autoloaders, because if you (or a library you use) register another one, it will never get called.
    – KingCrunch
    Nov 18, 2012 at 20:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.