Last active
December 11, 2015 07:19
-
-
Save FabienArcellier/4565573 to your computer and use it in GitHub Desktop.
A PSR-0 autoloader compatible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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