Why Composer’s Classmap‑Authoritative Matters in Production
Learn how Composer’s `--classmap-authoritative` flag removes runtime directory scans, the trade‑offs it introduces, and how to integrate it into a CI/CD pipeline for faster PHP requests.
05 Aug 2026, 04:25 UTC

Concrete Problem: Runtime Autoloader Overhead
When a PHP request triggers a class that Composer hasn’t seen before, the autoloader must walk the filesystem to find the file that implements that class. In a typical project with dozens of PSR‑4 namespaces, each class load can involve a stat call to the disk. On a network‑mounted filesystem or a very large vendor tree, those calls add measurable latency to every request.
In a staging environment this overhead is negligible, but in production—especially under load—every millisecond counts. The question becomes: can we eliminate those per‑class filesystem lookups without breaking the autoloader’s flexibility?
Thesis: Static Classmaps Are the Answer
Composer can generate a static mapping from class names to file paths. Once that map is loaded, the autoloader no longer needs to probe the filesystem for each request. Two Composer features enable this: --optimize (or optimize-autoloader: true) and --classmap-authoritative. The former creates a classmap but still falls back to PSR‑4 scanning if a class is missing; the latter removes that fallback entirely.
How Composer Builds the Classmap
Step 1 – Define PSR‑4 in composer.json
{
"autoload": {
"psr-4": {
"App\\": "src/",
"Vendor\\Package\\": "vendor/package/src/"
}
}
}
Composer reads this section and knows that a class App\Controller\HomeController lives in src/Controller/HomeController.php. By default, the autoloader will perform a file_exists check on that path at runtime.
Step 2 – Run composer dump-autoload --optimize
# Run in the project root
composer dump-autoload -o
Composer scans every directory listed under psr-4, finds all class files, and writes a static array to vendor/composer/autoload_classmap.php. The autoloader now looks up the class name in that array and includes the file directly.
Step 3 – Add --classmap-authoritative
composer dump-autoload -o --classmap-authoritative
With this flag, the autoloader will only consult the classmap. If a requested class isn’t present, the autoloader stops with a fatal error instead of trying the PSR‑4 directories. This eliminates any stat calls after the initial classmap load.
Concrete Worked Example
Assume a simple project structure:
src/
├─ Controllers/
│ └─ HomeController.php
vendor/
└─ composer.json
Contents of src/Controllers/HomeController.php:
After running composer dump-autoload -o --classmap-authoritative, the generated autoload_classmap.php will contain an entry similar to:
return array (
'App\\Controllers\\HomeController' => '/path/to/project/src/Controllers/HomeController.php',
);
Now, when a request calls new App\Controllers\HomeController(), the autoloader simply reads the array and includes the file—no filesystem lookups.
Trade‑Offs and Limitations
- Deployment Discipline: The classmap must be regenerated after every code change that adds or removes classes. In a CI/CD pipeline, this means running the dump command as part of the build step, not just on the developer machine.
- No Runtime Class Generation: Libraries that generate classes on the fly (e.g., Doctrine proxies) will break because the autoloader will not find the newly created class files. In such cases, you must disable
classmap-authoritativeor ensure those classes are pre‑generated. - Versioning Sensitivity: If you update a dependency that adds a new class, you must re‑dump the autoloader. Forgetting to do so can result in hard‑to‑debug “class not found” errors.
- Performance Gains Are Incremental: The reduction in
statcalls is beneficial, but the overall request time improvement is modest unless you have a very large vendor tree or a slow filesystem.
These trade‑offs are acceptable in most production environments where stability outweighs a few extra milliseconds per request.
Practical Checklist for CI/CD
- In your build container, run
composer install --no-dev --optimize-autoloader --classmap-authoritative. - Verify that
vendor/composer/autoload_classmap.phpcontains all expected classes. - Deploy the built artifact (e.g., a Docker image) to your staging or production servers.
- Optionally, enable PHP OPcache with
opcache.enable=1andopcache.save_comments=0to cache the compiled bytecode and further reduce load times.
By integrating the dump step into your CI pipeline, you guarantee that every deployment ships with a fresh, authoritative classmap that matches the codebase at that moment.
Actionable Closing
To decide whether classmap-authoritative is right for you:
- Measure your current filesystem I/O during a typical request with
strace -e stat -p $(pidof php-fpm)or a profiler. - Run a small benchmark by toggling the flag in a staging environment and comparing request times.
- If the improvement is noticeable and you can enforce a strict deployment pipeline, enable the flag in production.
- Otherwise, keep
--optimizealone to retain fallback flexibility.
Implementing an authoritative classmap is a low‑cost, high‑reliability change that can shave milliseconds off each request and reduce filesystem load—especially valuable on shared hosts or network‑mounted filesystems.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.