rfc:autoload_classmap
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:autoload_classmap [2021/03/15 17:13] – created marandall | rfc:autoload_classmap [2021/04/06 05:32] (current) – marandall | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Autoload Classmap ====== | ====== PHP RFC: Autoload Classmap ====== | ||
- | * Version: | + | * Version: |
* Date: 2021-03-15 | * Date: 2021-03-15 | ||
- | * Author: Mark Randall, | + | * Author: Mark Randall, |
- | * Status: | + | * Status: |
* Target: 8.1 | * Target: 8.1 | ||
Line 12: | Line 12: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC would introduce a new mechanism for autoloading classes via an internal classmap loader, that can be configured once per request by passing an associative array mapping | + | This RFC would introduce a new mechanism for autoloading classes via an internal classmap loader, that can be configured once per request by passing an associative array mapping |
- | This proposal is written with the names autoload_xx rather than spl_autoload_ as an independent RFC will be proposed to alias the existing SPL autoload functions to simply autoload_xx. If that RFC fails, but this one passes, the implementation would use spl_autoload_xx names instead. | + | This proposal is written with the names autoload_xx rather than spl_autoload_ as an independent RFC will be proposed to alias the existing SPL autoload functions to simply autoload_xx |
+ | <code php> | ||
/* Sets the internal autoloader to use the given classmap */ | /* Sets the internal autoloader to use the given classmap */ | ||
- | autoload_set_classmap(array $mapping); | + | autoload_set_classmap(array $mapping): void {} |
- | * Setting | + | /* Returns |
- | * The associative | + | autoload_get_classmap(): |
+ | </ | ||
- | /* Returns | + | Rules: |
- | autoload_get_classmap(): | + | |
+ | * The associative | ||
The autoloader will: | The autoloader will: | ||
- | * Check the classmap first if it is set. | + | |
- | * (If Found) Trigger a TypeError if the relevant | + | * (If Found) Trigger a TypeError if the key' |
- | * (If Found) Trigger an Error if the relevant class is not defined after including the file specified in the key value. | + | * (If Found) Use the REQUIRE mechanism. |
- | * (If Found) Use the REQUIRE_ONCE mechanism. | + | |
+ | |||
+ | If the classmap is not set, or the class name cannot be found within the classmap, the autoloader will continue to perform its traditional behaviour of iterating over the functions specified by spl_autoload_register. | ||
+ | |||
+ | ===== Performance ===== | ||
+ | |||
+ | Testing suggests that autoloading through an internal classmap delivers around 5% performance increase vs a userland function call (e.g. composer). | ||
+ | |||
+ | This is 5% of the cost of the autoloading, | ||
+ | |||
+ | Average for internal classmap was 0.295 vs 0.313 for spl_autoload_register representing 5.9% difference (in autoload performance only). | ||
- | If the classmap is not set, or the class name cannot be found within the classmap, the autoloader will continue to perform its traditional behaviour of iterating over the functions specified by spl_autoload_register. | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |
rfc/autoload_classmap.1615828406.txt.gz · Last modified: 2021/03/15 17:13 by marandall