Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2015 21:37
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 anonymous/7967178094314b357f61 to your computer and use it in GitHub Desktop.
Save anonymous/7967178094314b357f61 to your computer and use it in GitHub Desktop.
class Autoloader
{
public static $paths = array();
public static $files = array();
public static function register() {
spl_autoload_register(array(__CLASS__, 'load'));
}
public static function unregister() {
spl_autoload_unregister(array(__CLASS__, 'load'));
}
public static function path($paths) {
if (!is_array($paths)) {
$paths = array($paths);
}
foreach ($paths as $path) {
static::$paths[] = rtrim(realpath($path), DIR_SEP).DIR_SEP;
}
}
private static function load($class) {
$file = (false !== strpos($class, '\\')) ? strtolower(str_replace('\\', DIR_SEP, $class)) : $class;
foreach (static::$paths as $path) {
static::loadSingle($path.$file.'.php');
}
}
public static function loadSingle($file, $extract = null) {
if ($extract) {
extract($extract);
}
if (is_readable($file)) {
return require static::$files[] = $file;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment