{ PHP Autoloading PSR-0 }

I built a reusable SPL autoloader based on the PSR-0 standards awhile ago, due to its relative simplicity so I can drop it into something like a WordPress theme or plugin. It’s still viable and works well, but note that it is considered to be deprecated by the PHP FIG.  Their recommendation is PSR-4, which will look familiar to the Laravel / Symfony / Drupal 8 devs.

Here is the class, which assumes this directory structure:

-- Application
  -- Autoload
    -- Loader.php

If you’re getting an exception thrown because there are other autoloaders present outside of your application, you can add some code here to ignore certain file / class patterns or comment it out:

if (!$success) {
   if (!self::loadFile(__DIR__ . DIRECTORY_SEPARATOR . $fn)) {
     //throw new \Exception(self::UNABLE_TO_LOAD . ' ' . $class);
     error_log('Unable to load ' . $class);
   }
}

Then, put a file to instantiate the autoloader in the directory above Application, even something as simple as this:

autoloader.php

Sometimes you don’t want the overhead of Composer, or can’t use it in some cases but I’d recommend using it whenever possible. It caches the references to the classes, speeding things up a bit.