Skip to content

Instantly share code, notes, and snippets.

@FabienArcellier
Last active December 11, 2015 07:19
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 FabienArcellier/4565573 to your computer and use it in GitHub Desktop.
Save FabienArcellier/4565573 to your computer and use it in GitHub Desktop.
A PSR-0 autoloader compatible
<?php
/**
* Autoload function for PHP. (Compatible PSR-0)
* Use automatic include base on directory
*/
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($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;
}
/**
* Add a directory to include path for Autoload
* @param string $path
*/
function add_include_path ($path)
{
foreach (func_get_args() AS $path)
{
if (!file_exists($path) OR (file_exists($path) && filetype($path) !== 'dir'))
{
trigger_error("Include path '{$path}' not exists", E_USER_WARNING);
continue;
}
$paths = explode(PATH_SEPARATOR, get_include_path());
if (array_search($path, $paths) === false)
array_push($paths, $path);
set_include_path(implode(PATH_SEPARATOR, $paths));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment