Question

I'm trying to register a few autoloaders and I get an HTTP 500. My error log says the following:

[05-Aug-2013 04:32:38 UTC] PHP Fatal error: Uncaught exception 'LogicException' with message 'Function 'Autoloader::config' not callable (non-static method Autoloader::config() should not be called statically)' in /home2/canforce/public_html/index.php:5

There was some stack trace part on the end of the error log, but it came showed up in huge letters so I took it out, I didn't think it was important.

I think my autoloader should work based on what I've read but for some reason it doesn't, here's the code:


index.php

include("config/autoloader.php");
spl_autoload_register('Autoloader::config');
spl_autoload_register('Autoloader::controller');
spl_autoload_register('Autoloader::service');



config/autoloader.php

class Autoloader {
function config($class) {
    $file = 'config/' . $class . '.php';
    if(file_exists($file)) {
        require_once $file;
    }
}
function controller($class) {
    $file = 'presentation/controllers/' . $class . '.php';
    if(file_exists($file)) {
        require_once $file;
    }
}
function service($class) {
    $file = 'model/services/' . $class . '.php';
    if(file_exists($file)) {
        require_once $file;
    }
}
}
Was it helpful?

Solution

You need to create an instance of the Autoloader class and then pass it to the register function.

include("config/autoloader.php");
$autoloader = new Autoloader();
spl_autoload_register(array($autoloader, 'loader'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top