Skip to content

Instantly share code, notes, and snippets.

@jesseschalken
Last active March 3, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesseschalken/26de0fb81b63452f27f5 to your computer and use it in GitHub Desktop.
Save jesseschalken/26de0fb81b63452f27f5 to your computer and use it in GitHub Desktop.
This autoloader will search for Foo\Bar\Baz (for example) in ./Foo/Bar/Baz.php, ./Foo/Bar.php and ./Foo.php, in that order. This enables developers to place multiple classes and even multiple namespaces in one file when appropriate. Just place this file in your src/ directory and use it as the entry point for your project.
<?php
spl_autoload_register(function ($cls) {
for ($cls = explode('\\', $cls); $cls; array_pop($cls)) {
$sep = DIRECTORY_SEPARATOR;
$php = __DIR__ . $sep . join($sep, $cls) . '.php';
if (file_exists($php)) {
/** @noinspection PhpIncludeInspection */
require_once $php;
break;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment