I would like to propose the addition of a new mechanism of autoloading
classes - a classmap that will be consulted prior to checking the
spl_autoload_register'd callbacks.
My initial reaction is (can you guess?): This can be done in userspace.
function autoload_classmap(array $classes): void {
spl_autoload_register(fn(string $class): void {
if (isset($classes[$class])) require($classes[$class]);
});
}
This is essentially what composer already does for class maps, and it seems
to work quite well there.
However, I can see some small performance benefit from not having to invoke
a userspace function (very small), so it's not wholly without merit.
Where I have more issue is with case folding. I see three options:
1/ Require users to treat their class names as case-sensitive if they're
going to use a class map. Personally, I like that this forces users away
from case-insensitivity without breaking existing apps.
2/ Require users to pass a map with folded classnames. I expect the
significant majority of users of this are using machine generated maps, so
doing the case fold at "compile" time should be trivial.
3/ Allow any case in the map and fold it to lower at set time. This is the
worst option IMO and no matter how it's implemented it's going to negate
any performance gain it introduces. If someone REALLY wants that, they
can map their array before invoking autoload_classmap(). This also forces
them to make a decision about conflict resolution themselves.
-Sara