Skip to content

Instantly share code, notes, and snippets.

@sshilko
Last active January 14, 2016 20:00
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 sshilko/edaf7863d4738fb51868 to your computer and use it in GitHub Desktop.
Save sshilko/edaf7863d4738fb51868 to your computer and use it in GitHub Desktop.
autoload_psr0_lazy
<?php
/**
* PSR-0 Autoloader
* http://www.php-fig.org/psr/psr-0/
* works PHP5 & PHP7
* @sshilko
*/
function autoload_psr0($className)
{
$oname = $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';
$filePath = stream_resolve_include_path($fileName);
if ($filePath) {
/**
* put your mostly used files high in include_path
* some frequent packages are directly mapped
* to prevent full composer autoloaded
*/
return include $filePath;
} else {
/**
* Only need autoloader once
* check correct paths
*/
if (!isset($GLOBALS['composer_autoloaded'])) {
$GLOBALS['composer_autoloaded'] = true;
$loader = require __DIR__ . '/../composer/autoload.php';
$loader->loadClass($oname);
}
return false;
}
}
//include __DIR__ . '/../composer/autoload.php';
spl_autoload_register('autoload_psr0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment