Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Created September 11, 2013 19:21
Show Gist options
  • Save pjdietz/6528491 to your computer and use it in GitHub Desktop.
Save pjdietz/6528491 to your computer and use it in GitHub Desktop.
PSR Autoloader
<?php
/**
* Autoload function adapted from from PSR-0 standard.
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md#splclassloader-implementation
*/
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', '/', $namespace) . '/';
}
$fileName .= str_replace('_', '/', $className) . '.php';
require $fileName;
}
// To apply, call:
spl_autoload_register('autoload');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment