autoload.php
27 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Simple autoloader that follow the PHP Standards Recommendation #0 (PSR-0) |
| 5 | * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md for more informations. |
| 6 | * |
| 7 | * Code inspired from the SplClassLoader RFC |
| 8 | * @see https://wiki.php.net/rfc/splclassloader#example_implementation |
| 9 | */ |
| 10 | spl_autoload_register(function ($className) { |
| 11 | $className = ltrim($className, '\\'); |
| 12 | $fileName = ''; |
| 13 | if ($lastNsPos = strripos($className, '\\')) { |
| 14 | $namespace = substr($className, 0, $lastNsPos); |
| 15 | $className = substr($className, $lastNsPos + 1); |
| 16 | $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; |
| 17 | } |
| 18 | $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php'; |
| 19 | if (file_exists($fileName)) { |
| 20 | require $fileName; |
| 21 | |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | return false; |
| 26 | }); |
| 27 |