Optimizing Composer Autoloading for Production with Classmap-Authoritative Mode
Switch Composer to classmap-authoritative mode to eliminate filesystem scans during class loading in production. This guide covers enabling the setting, regenerating the optimized autoloader, verifying the classmap covers all classes, and rollback steps if dynamic class discovery breaks.
28 Oct 2025, 18:08 UTC

Desired Outcome
Reduce autoloader overhead in production by switching Composer to classmap-authoritative mode, eliminating filesystem scans for every class load while maintaining reliable class resolution.
Prerequisites
- Composer 2.x installed and available in PATH
- A PHP project with
composer.jsondefining PSR-4 autoloading - All production classes already discovered by Composer (run
composer installat least once) - No runtime class generation or dynamic
requirestatements outside the vendor directory
Procedure
1. Verify Current Autoloader Configuration
Inspect the generated autoloader to confirm PSR-4 mappings are correct:
cat vendor/composer/autoload_psr4.php
You should see an array mapping namespace prefixes to directory paths, for example:
return array(
'App\\' => array($baseDir . '/src'),
'Vendor\\Package\\' => array($vendorDir . '/vendor/package/src'),
);
If mappings are missing or point to wrong directories, fix composer.json first.
2. Enable Classmap-Authoritative Mode
Add the classmap-authoritative flag to the config section of composer.json:
{
"config": {
"classmap-authoritative": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
This tells Composer to trust the generated classmap completely and skip fallback directory scanning.
3. Regenerate the Optimized Autoloader
Run the dump-autoload command with optimization flags:
composer dump-autoload -o --classmap-authoritative
The -o flag enables classmap generation (converting PSR-4 rules to a flat class-to-file map). The --classmap-authoritative flag reinforces the config setting during this specific run.
4. Verify the Generated Classmap
Check that vendor/composer/autoload_classmap.php contains entries for all your project classes:
grep -c "App\\" vendor/composer/autoload_classmap.php
Compare the count with your actual class files:
find src -name "*.php" -type f | wc -l
Numbers should match. Missing entries indicate classes not discovered during the dump.
Expected Checks
- Autoloader loads classes without stat calls: Use
strace -e trace=stat php -r "require 'vendor/autoload.php'; new App\\ExampleClass();" 2>&1 | grep -c stat— should show zero stat calls for known classes. - Application boots correctly: Run your test suite or manually exercise critical paths.
- No "Class not found" errors: Check logs for any missing class exceptions.
Recovery Options
If classmap-authoritative mode breaks class loading:
Immediate Rollback
composer config classmap-authoritative false
composer dump-autoload -o
This restores PSR-4 fallback scanning while keeping the optimized classmap.
Diagnose Missing Classes
If specific classes fail to load, they were not included in the classmap. Common causes:
- Classes added after the last
dump-autoloadrun - Classes in directories not covered by PSR-4 rules
- Classes loaded dynamically via
requireorincludeoutside autoloader
Fix by updating composer.json autoload section or adding a classmap entry for non-PSR-4 directories:
"autoload": {
"psr-4": {"App\\": "src/"},
"classmap": ["legacy/"]
}
Then regenerate: composer dump-autoload -o --classmap-authoritative.
Limitations
- No dynamic class discovery: Any class not in the classmap at dump time will not load. This breaks libraries that generate classes at runtime (e.g., Doctrine proxies, some ORM entity generators).
- Development workflow impact: Developers must run
composer dump-autoload -oafter adding new classes. Consider disabling classmap-authoritative in localcomposer.jsonviacomposer config classmap-authoritative falseand enabling only in CI/production. - Plugin compatibility: Some Composer plugins that manipulate autoloading may conflict. Test thoroughly.
Practical Verification
Add a CI step to verify the optimization works:
# .github/workflows/autoload-check.yml
- name: Verify classmap-authoritative
run: |
composer dump-autoload -o --classmap-authoritative
php -r "
require 'vendor/autoload.php';
$missing = [];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('src')) as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$content = file_get_contents($file->getPathname());
if (preg_match('/^namespace\\s+([^;{]+)/m', $content, $m)) {
$ns = trim($m[1]);
$class = $ns . '\\' . basename($file->getPathname(), '.php');
if (!class_exists($class) && !interface_exists($class) && !trait_exists($class)) {
$missing[] = $class;
}
}
}
}
if ($missing) {
echo 'Missing from classmap: ' . implode(', ', $missing) . PHP_EOL;
exit(1);
}
echo 'All source classes loadable.' . PHP_EOL;
"
This catches missing classes before deployment.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.