Created
September 28, 2014 08:42
-
-
Save thiphariel/ee08db39c53c0294979b to your computer and use it in GitHub Desktop.
Light autoloader
This file contains hidden or 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 | |
| /** | |
| * Autoloader class | |
| * @author Thomas Collot | |
| */ | |
| namespace Light\App; | |
| class Autoload | |
| { | |
| /** | |
| * Register the autoloader | |
| */ | |
| public function register() | |
| { | |
| spl_autoload_register(array(__CLASS__, 'autoload')); | |
| } | |
| /** | |
| * Light Autoload | |
| */ | |
| private function autoload($class) | |
| { | |
| $class = ltrim($class, '\\'); | |
| $fileName = ''; | |
| $namespace = ''; | |
| if ($lastNsPos = strripos($class, '\\')) { | |
| $namespace = substr($class, 0, $lastNsPos); | |
| $class = substr($class, $lastNsPos + 1); | |
| $fileName = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR); | |
| } | |
| $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; | |
| $fileName = strtolower(substr($fileName, 6)); | |
| require_once '../' . $fileName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment