Implement function autoloading (mark 5) - #22337
Conversation
da508ed to
2904317
Compare
1fe448a to
99e4623
Compare
|
I can understand the reluctance of the others to implement this and prior RFCs on the grounds of performance since it's hardly the case there would be more classes than functions being loaded, but it can be argued that the performance penalties in this regard can be mitigated by implementing namespace-level loading within the coder's callback itself: function fn_autoload(string $fn):void{
// Base path for finding modules
$path=__DIR__.\DIRECTORY_SEPARATOR.'modules';
// Trim, normalize and build base file path string
$fn = $path.\DIRECTORY_SEPARATOR.\strtolower(\ltrim($fn,'\\/'));
//Use namespace-based or function-based loading?
$nsmode=true;
$fn= ($nsmode ? \dirname($fn) : $fn). '.func.php';
// Include the file! (contains both `ExistsDir` and `ExistFile`)
@include_once $fn;
}
//
// Place callback-registering function call here
//
// Examples
\IO\ExistsFile('/path/to/file.txt'); // Autoload triggers
\IO\ExistsDir('/path/to/directory'); // No autoload triggerIn this example, when mentioning This approach would not eliminate all of the performance penalties, but would create an opening to give the coder the option to manipulate within the callback to load beyond the scope of a single function, because it's all about the timing of what is encountered first. Natively locking the engine itself to namespace-based loading may be too restrictive; in some usage scenarios, loss of performance for function-level auto-loading may be a necessary or negligible trade-off. |
RFC at https://wiki.php.net/rfc/function-autoloading-five-oh