Composer's autoload-dev: Separating Test Infrastructure from Production PHP Code
Composer's autoload-dev feature separates test infrastructure from production code, reducing memory usage and preventing accidental test execution in production. Here's how to configure it correctly.
06 Jun 2026, 19:22 UTC

The Problem: Bloated Production Autoloaders
When you install a PHP project with Composer, every class in your vendor/ directory gets loaded into the autoloader—even test classes, mocks, and development tools that should never run in production. This bloats memory usage and can expose sensitive test infrastructure. The solution isn't manual cleanup; it's proper separation using Composer's autoload-dev feature.
How autoload-dev Works
The autoload-dev section in composer.json defines class loading rules exclusively for development dependencies. When you install with --no-dev, Composer completely skips these mappings, leaving only your production classes in the autoloader.
Key benefits:
- Reduced memory footprint—test classes aren't loaded
- Security improvement—test infrastructure stays out of production
- Cleaner dependency graph—dev tools won't accidentally leak into production builds
Practical Configuration Example
Here's a typical setup for a project with separate test and source directories:
{
"name": "example/project",
"autoload": {
"psr-4": {
"Example\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Example\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^10.0"
}
}
This configuration maps Example\ classes to src/ and test classes to tests/. The critical part: production code must never reference classes in the Example\Tests\ namespace.
Verification Process
To confirm your setup works correctly:
- Full installation: Run
composer installand verify test classes load:php -r "require 'vendor/autoload.php'; new Example\\Tests\\SomeTest();" - Production installation: Run
composer install --no-devand verify test classes are unavailable:php -r "require 'vendor/autoload.php'; new Example\\Tests\\SomeTest();"You should see a
Class 'Example\\Tests\\SomeTest' not founderror.
Critical Limitations and Trade-offs
The hard rule: Never let production code depend on autoload-dev classes. If your application loads test fixtures or mocks at runtime, it will crash in production.
Performance impact: While autoload-dev reduces the autoloader size, the real performance gain comes from omitting development dependencies entirely. A typical test suite with PHPUnit, mocks, and fixtures can add hundreds of files to the autoloader.
Deployment verification: Always test your production build locally with --no-dev before deploying. CI/CD pipelines should include this check to catch accidental dev dependencies.
Actionable Checklist
- Add
autoload-devsection tocomposer.jsonwith your test namespace mappings - Verify no production code references test namespaces
- Test both
composer installandcomposer install --no-dev - Update deployment scripts to use
--no-devflag - Consider adding a CI job that validates the production autoloader contains no dev classes
Using autoload-dev isn't just organization—it's a deployment safety mechanism that prevents test infrastructure from accidentally running in production.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.