phpautoloadautoloaderspl-autoload-registerspl-autoloader

PHP - Autoloader doesn't work when registering it EDIT


I am trying to autoload a class, but my autoloader doesn't seem to register properly.

Folder/File structure:

**zest.php**:

<?php
    $aThing = new a;
    $aThing->test();

    function my_autoloader($class) {
        include 'aest/' . $class . '.thing.php';
    }

    spl_autoload_register('my_autoloader');
?>

**a.thing.php**:

<?php
class a {
    public function test() {
        echo 'test';
    }
}

I pulled this example straight from php.net, what is wrong with it?

The autoloader function doesn't get called at all.

Not even when it is an anoymous function:

spl_autoload_register(function($class) {
    echo 'calling '.$class;
    include 'aest/'.$class . '.test.php';
});

Solution

  • Ughh... spl_autoload_register('AutoLoader') has to be called before any attempt to instantiate a class that has to be autoloaded is made.