Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emresaracoglu/35e0181b42aadd118862ee253fa69343 to your computer and use it in GitHub Desktop.
Save emresaracoglu/35e0181b42aadd118862ee253fa69343 to your computer and use it in GitHub Desktop.

Revisions

  1. @erikjung erikjung created this gist Jul 9, 2012.
    40 changes: 40 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php

    // Probably unnecessary, but wanted to test the waters of 5.4

    trait NamespaceConverter
    {
    function nsToPath($class) {
    return str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
    }
    }

    // Namespace-based paths (e.g. "new Foo\Component\Bar()" => "/Foo/Component/Bar.php")

    class Autoloader
    {
    use NamespaceConverter;

    public $basePath;

    public function __construct($basePath = '.')
    {
    $this->basePath = realpath($basePath);
    spl_autoload_register([$this, 'load']);
    }

    public function load($class)
    {
    $file = $this->basePath . DIRECTORY_SEPARATOR . $this->nsToPath($class);
    if (file_exists($file)) include $file;
    }
    }

    /*
    Usage:
    $loader1 = new Autoloader('path/to/some/core/classes');
    $loader2 = new Autoloader('path/to/some/vendor/classes');
    */