Pruning Your Production Autoloader with Composer autoload-dev
Stop bloating your production autoloader. Learn how to use Composer's autoload-dev to separate test namespaces from application logic for better performance and cleaner builds.
09 Jan 2026, 14:17 UTC

The Cost of a Bloated Autoloader
In a typical PHP project, your src/ directory contains the logic that powers your application, while your tests/ directory contains the logic that proves it works. However, by default, many developers register both directories in the same way. This means that in a production environment, the PHP autoloader is still tracking, mapping, and searching for test suites, mock objects, and seeders that will never be executed.
The takeaway is simple: your production environment should not know your test classes exist. Using the autoload-dev property in composer.json allows you to decouple these namespaces, reducing the memory footprint of the autoloader and preventing development-only code from leaking into the runtime path.
Separating Concerns in composer.json
Composer provides two distinct sections for defining how classes are loaded: autoload and autoload-dev. The autoload section is for the core application logic required for the app to function. The autoload-dev section is specifically for classes required only for development, such as PHPUnit test cases or data factories.
When you run a standard composer install, both sections are merged into the generated autoloader. However, when you deploy to production, you can instruct Composer to ignore the development definitions entirely. This ensures that the class map remains lean and that the application doesn't waste cycles resolving namespaces that aren't needed.
Practical Implementation
To implement this, you must split your PSR-4 mappings. Instead of putting all directories under a single autoload key, move your testing namespaces to autoload-dev.
Example Configuration
{
"name": "company/project",
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
}
}
Deployment Execution
To ensure the development classes are excluded from the production build, run the following command in your CI/CD pipeline or on your production server. This requires the composer binary to be installed and the user to have write permissions to the vendor/ directory.
# Run this on the production server or build agent
composer install --no-dev --optimize-autoloader
What this does:
--no-dev: Skips installation of packages listed inrequire-devAND ignores theautoload-devrules.--optimize-autoloader: Converts PSR-4 lookups into a flat class map (an associative array), which is significantly faster than scanning the filesystem.
Verification and Risks
You can verify the result by attempting to instantiate a test class in a production-like environment. If the configuration is correct, PHP should throw a Fatal error: Uncaught Error: Class 'App\Tests\UserTest' not found.
The "Missing Class" Trap
The primary risk of using autoload-dev is the accidental dependency. If a production class in src/ references a helper class located in tests/, your application will work perfectly in your local environment (where dev-autoload is active) but will crash immediately in production.
To mitigate this, always run your automated test suite against a build that has been processed with --no-dev before the final deployment. This catches "leaked" dependencies before they hit your users.
Performance Trade-offs
While the performance gain per request is small, the cumulative effect in high-traffic applications is measurable. A smaller class map means less memory usage per PHP process and faster lookups. When combined with --optimize-autoloader, you eliminate the need for the autoloader to check the disk for the existence of development files that aren't even present on the production server.
Summary Checklist for Production
- Move
tests/andmocks/toautoload-dev. - Use
composer install --no-devin the deployment pipeline. - Apply
-oor--optimize-autoloaderto flatten the remaining map. - Verify that no production code depends on dev-autoloaded classes.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.