Last active
January 22, 2024 14:45
-
-
Save adriengibrat/4761717 to your computer and use it in GitHub Desktop.
Extreme minification of shortest possible PSR-0 compliant autoloader, 5 lines !
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 | |
//set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__); // optional | |
spl_autoload_register(function ($class) { | |
$file = preg_replace('#\\\|_(?!.+\\\)#','/', $class) . '.php'; | |
if (stream_resolve_include_path($file)) | |
require $file; | |
}); |
Yes, autoloading happens only if the class definition is not yet included. But may be require_once is safer ;)
This regex is reliable?
Some tests indicate an error in this part "(?!".
For the perfect, please add braces to if statement :))
"The body of each structure MUST be enclosed by braces." (c) PSR-2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not use
require_once
? Or doesspl_autoload_register
automatically prevent loading files multiple times?