Skip to content

Instantly share code, notes, and snippets.

@joostvanveen
Created June 9, 2017 06:37
Show Gist options
  • Save joostvanveen/f92b73e65079987bbc8d7feb62e2e317 to your computer and use it in GitHub Desktop.
Save joostvanveen/f92b73e65079987bbc8d7feb62e2e317 to your computer and use it in GitHub Desktop.
PSR-0 PHP autoloader
<?php
/**
* PSR-0 autoloader
* @param string $className
*
* @see http://www.php-fig.org/psr/psr-0/
*/
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
/**
* Register the PSR-0 autoloader we created above
*/
spl_autoload_register('autoload');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment