Last active
September 2, 2017 22:06
-
-
Save BrainInBlack/2bd27c00c39ec61a667c to your computer and use it in GitHub Desktop.
Autoloader Closure for PHP 7.x+
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 | |
declare(strict_types=1); | |
/** | |
* Autoload Closure | |
* Simple autoload closure, that makes use of namespaces. | |
* @version 7.0.1 | |
* @author Daniel "BrainInBlack" Lieberwirth <[email protected]> | |
* @license DO WHAT EVER THE HECK YOU WANT | |
*/ | |
spl_autoload_register(function(string $v) { | |
include('.'.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $v).'.php'); | |
}); |
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
Description: | |
According to my research, this is the absolute minimum for a working, | |
autoloader, that skips several steps that slow down the loading process. | |
For example, usualy people like to use inlude_once() or require_once(), | |
instead of just include or require. But this step is not neccessary, | |
since spl is aware what is loaded and what not, so why waste time? | |
The rest is "as usual", but boiled down to the absolute minimum. If the | |
loader failed, you failed to use a propper filestructure. It's that simple! | |
Usage: | |
As you can see, the loader will always start from the cwd(), i.e. | |
"\App\Model\Class" will be translated to cwd()."App/Model/Class.php". | |
Even though the cwd() function is not being used by the loader, it will | |
allways use the cwd since there is a "." (dot) prepended to the path. | |
(See INLCUDE_PATH in the PHP-Manual) | |
History: | |
v7.0.1 - Minor update for 7.x Type Hinting | |
v2.0.1a - First public release (03/26/2015) |
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
DO WHAT EVER THE HECK YOU WANT | |
Comercial use: permitted | |
Private use: permitted | |
Modification: permitted | |
Credit: not required, but very welcome | |
This Programm/Snippet/Code is provided as is. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment