Help with Autoload()

Long story short: I’m working on redesigning an existing application, with a full rewrite. As a result, I’m forcing myself to stick to strict OOP, and something of a light MVC pattern as well. Since requiring individual files is tedious, I’m looking to integrate some sort of autoload function, using spl_autoload_register().


class Controller extends SingletonObject {
    private $registry;
    private $model;
    private $view;
    private $instance;

    private function __construct(){
        spl_autoload_register(array($this, "myAutoLoadFunction"));

    public static function singleton(){
            $c = __CLASS__;
            $class = new $c();
            self::$instance = $class;
        return self::$instance;

    public static function myAutoLoader($name){
        define(CLASS_PATH, "/var/www/html/portal/dev/");
        try {
            if(is_readable(CLASS_PATH . "/classes/" . $name . ".php")) {
                require_once(CLASS_PATH . "/classes/" . $name . ".php");
            } elseif(is_readable(CLASS_PATH . "/interfaces/" . $name . ".php")) {
                require_once(CLASS_PATH . "/interfaces/" . $name . ".php");
            } elseif(is_readable(CLASS_PATH . "/pages/" . $name . ".php")) {
                require_once(CLASS_PATH . "/pages/" . $name . ".php");
            } else {
                throw new Exception("The file you are attempting to access does not exist.");
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();


Unfortunately, I keep getting the following error wherever I try to instantiate the controller:

Fatal error: Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static method' in /var/www/html/portal/dev/classes/Controller.php:15 Stack trace: #0 /var/www/html/portal/dev/classes/Controller.php(15): spl_autoload_register(Array) #1 /var/www/html/portal/dev/classes/Controller.php(21): Controller->__construct() #2 /var/www/html/portal/dev/classes/Session.php(82): Controller::singleton() #3 /var/www/html/portal/dev/pages/TestSession.php(7): Session->dumpSession() #4 {main} thrown in /var/www/html/portal/dev/classes/Controller.php on line 15

Does anyone have any suggestions on registering an autoload function that doesn’t require sticking a function floating out in a file somewhere? This one is stumping me.

I’m not entierly sure what you’re trying to attempt but here’s a snippet.

class Application_Autoloader
    public static function Register(){
        return spl_autoload_register(array($this, 'Load'));
    public static function Load($object_name){
        #resolve path
        #does file exist


Bear in mind too, that prior to 5.3, you cannot throw an exception from a registered autoload implementation.

What I was trying to achieve was declaring the autoloader inside a class constructor.

The issue I was running into was a simple one: I named the function myAutoLoader, and called myAutoLoadFunction.

After correctly renaming the function, I was able to set the autoload inside the constructor for Controller (which is designed as the entry point for every page in the application, so it should always be loaded). So, what you posted above is more or less accurate, I just wanted to make it something that happened without me thinking about it during the normal course of a day.

The information about not being able to throw an exception is useful. Would the correct course of action there be to raise a fatal error, then?

yes as $this refers to the current instance of the object