Composer autoloader class map and PHP runtime resolution precedence
0 reputation · 09 Aug 2023, 17:38 UTC
0 reputation · 09 Aug 2023, 17:38 UTC
The goal is to clarify how Composer’s autoloading configuration interacts with PHP runtime class resolution when optimized class maps, PSR-4 and PSR-0 rules coexist for the same code base.
The integration boundary involves Composer generating static maps and PSR rules that PHP’s SPL autoloader consumes at request time. There is uncertainty about precedence when multiple autoload definitions can resolve the same fully qualified class name, and about how the APCu autoloader feature changes resolution behavior versus caching results. A decision point is whether optimization is warranted without first confirming that filesystem lookups are a measurable bottleneck in the specific runtime.
What is the documented precedence order between class map entries, PSR-4 mappings and PSR-0 mappings when overlapping definitions exist?
Does enabling the APCu autoloader alter precedence or only cache the outcome of the existing resolution order?
Under which conditions does an optimized class map take precedence over a PSR-4 namespace lookup during PHP runtime autoloading?
32275 reputation · 10 Aug 2023, 03:36 UTC
Precedence order (when overlapping definitions exist):
composer dump-autoload --optimize or the default autoload_classmap.php)Enabling the APCu autoloader does not change this order; it only caches the file path that results from the standard lookup sequence.
An optimized class map takes precedence over a PSR‑4 lookup whenever the class name is present in the map. In that case Composer skips the PSR‑4 runtime check entirely.
autoload.php first includes autoload_classmap.php and checks the map.COMPOSER_APCU=true or --apcu) simply stores the resolved file path after the normal lookup; it does not reorder the lookup steps.composer dump-autoload --optimize is run, Composer writes a fully resolved map and removes the runtime PSR‑4/PSR‑0 checks for classes that are present in the map.composer dump-autoload --optimize to generate a full map.vendor/composer/autoload_classmap.php and confirm the target class is listed.require it and verify Composer performs a PSR‑4 lookup (e.g., by adding a debug log in autoload_psr4.php).COMPOSER_APCU=true composer install (or composer install --apcu). Trigger a class load and inspect the APCu cache with apcu_cache_info() or a custom script to see the cached file path.To tailor this advice further, could you confirm:
| Lookup Step | Type | Order |
|---|---|---|
| Class‑map | Static file map | 1 |
| PSR‑4 | Runtime namespace mapping | 2 |
| PSR‑0 | Legacy underscore mapping | 3 |
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.