I Have a platform writen in PHP, and store a class definition in each file in following way:
filename.php = lower(<class name>) + ".php"
I also use autoload functions to get class name and, using a json generated file (by a custom tool that I made), load the especific file that contains the class definition (I tried to use blob before this, but is too slow).
Why a generated file? Because doing a file search on diretory tree in every request to find the corresponding class is IO intensive.
my generated json class map:
{
"homecontroller": "path/to/dir/homecontroller.php",
"database": "path/to/another/deep/dir/database.php"
}
Today this process of loading json, decoding and store in a array to use in autoload to find classes takes arround 30~50 milliseconds.
autoload, using the generated json class map:
$_runtimefile = "packages/.runtime-map";
$classmap = json_decode(file_get_contents($_runtimefile));;
function __autoload($classname)
{
global $classmap;
$classidentifier = mb_strtolower($classname);
if(!array_key_exists($classidentifier, $classmap)) return;
$filename = __DIR__ . "/" . $classmap->{$classidentifier};
if(!file_exists($filename))
{
debug_print_backtrace();
throw new RuntimeException("File {$filename} not found for class {$classname}");
}
require_once($filename);
}
But I want to do this in a better way, in performance point of view. What are the better ways to load/have a class mapping or autoload for large projects and how I can do this?
Additional informations:
- The project has arround 300 classes.
- Currently, a simple request takes a minimum of 30~50 milliseconds in localhost.