phppsr-0spl-autoloader

How can I reconcile SplClassLoader's namespace requirement with my custom directory layout?


I recently starting writing a custom MVC framework in PHP. It's basically a learning exercise.

My classes are located in the following directories:

I'm not using namespaces because I can't figure out how to instantiate controllers using namespaces and Apache 2 handler style URLs (controller/action/id). I created a Bootstrap class to autoload my other classes:

class Bootstrap
{
  public function autoloadClasses($class)<br/>
  {
    $class .= '.php';

    $classDirectories = array(
        SYSTEM_LIBS_DIR,
        SYSTEM_CONTROLLERS_DIR,
        SYSTEM_MODELS_DIR,
        APPLICATION_LIBS_DIR,
        APPLICATION_CONTROLLERS_DIR,
        APPLICATION_MODELS_DIR
    );

    foreach ($classDirectories as $classDirectory) {
        $directoryIterator = new DirectoryIterator($classDirectory);
        foreach($directoryIterator as $file) {
            if ($file == $class) {
                include $classDirectory . $class;
                break 2;
            }
        }
    }

  }


  public function register()
  {
    spl_autoload_register(array($this, 'autoloadClasses'), true);
  }


  public function init()
  {
    $this->register();
    $loader = new Loader($_GET);
    $controller = $loader->createController();
    $controller->executeAction();
  }
}

It works fine. However, I know I should really be using the implementation recommended by PSR-0:

https://gist.github.com/221634

However, I can't figure out how to get it to work without namespaces. It looks like the namespace is an optional pararmeter. However, if I do the following, nothing happens -- not even an error in the Apache logs:

$libLoader = new SplClassLoader('', 'system/libraries');

Solution

  • The goal of PSR-0 was to try and specify how external third-party library classes should be named, and where the files containing those classes should live on disk. This goal was accomplished, and from a high level, it's not too bad of a thing. Interopability and not stepping all over other libraries is a good thing.

    Your directory layout and class naming scheme doesn't mesh with PSR-0, which means SplClassLoader is going to be nigh-useless for you.

    You have two options:

    1. Rename all of your classes, shuffle them into a namespace hierarchy, and refactor the rest of the code that needs to worry about it, or
    2. Don't use SplClassLoader and write your own autoloader.

    If you're building a library intended for external distribution, it'll be a good idea to make yourself PSR-0 compliant, as it's pretty darn simple, logical and painless.

    If you're building your own app for your own use and don't intend it as a library, then you are under no requirement to do all of that work, and you shouldn't, because it'd be silly. This looks like it's the case, so I can end with a big fat: don't bother.