Article
Adding Composer to a Legacy PHP Project
Introduce composer.json, a lockfile, and vendor/autoload.php into an existing application while keeping the current include bootstrap working.
Adding Composer is usually the first reversible modernization win: you gain a dependency contract without rewriting business logic. The goal of the first pull request is not a perfect PSR-4 tree—it is a committed composer.json/composer.lock, a generated vendor/ on deploy, and a single autoload require that coexists with the old bootstrap.
Before you run Composer
- Create a version-control branch and a filesystem/database backup of the environment you will change
- Identify the true application root (the directory that should own
composer.json, usually above the web document root) - Note the production PHP version and required extensions with
php -vandphp -mon the same SAPI you deploy - List third-party libraries currently copied into the tree—those become early
requirecandidates
/var/www/app/
public_html/ ← document root (index.php)
lib/
config/
templates/
third_party/phpmailer/
/var/www/app/
composer.json
composer.lock
vendor/
public/ ← document root
src/
config/
templates/
Minimal first composer.json
{
"name": "acme/legacy-app",
"description": "Internal application",
"type": "project",
"require": {
"php": ">=7.4"
},
"config": {
"platform": {
"php": "7.4.33"
},
"sort-packages": true
},
"autoload": {
"classmap": ["lib/"]
}
}
Set platform.php to the PHP version you actually run in staging/production so developers on newer laptops do not resolve packages you cannot install on the server. Raise the constraint when the runtime ladder moves—see platform constraints.
Wire the autoloader without deleting includes
install vs update
On servers and CI deploy jobs, run composer install --no-dev --optimize-autoloader from a committed lockfile. Run composer update only on a development branch when you intend to change versions. Do not treat update as a routine deploy step for a legacy app—transitive upgrades are modernization work that need tests.
Backup and rollback
Keep the previous release artifact or vendor directory available until the new deploy proves healthy. If autoload paths are wrong, the failure mode is often a flood of class-not-found fatals—revert the release rather than hot-editing vendor on the server.
Validate the JSON and platform story
- Composer.json Validator
- Composer Platform Requirement Checker
- Constraint Explainer
- PHP Environment Compare for local vs staging vs production
Next: includes → autoloading, PSR-4. Official intro: Composer getting started.
Related tools
- Composer.json Validator Validate composer.json structure and common mistakes without running composer install.
- Legacy PHP Risk Checker Paste PHP source for a static scan that classifies removed APIs, deprecated calls, and security-sensitive leg…
- PHP Deprecated Checker Find deprecated functions and patterns in pasted PHP to prioritize modernization work.
- PHP Environment Compare Compare two PHP environment summaries to find directive and extension mismatches.
- PHP Modernization Roadmap Build an ordered migration stage list from your PHP version, framework, Composer, database API, and deploymen…
- PHP Version Compatibility Checker Scan pasted PHP for version-sensitive syntax and APIs to plan upgrades across PHP releases.
Related reading
- Composer Modernization Center Add Composer to legacy PHP projects, migrate includes to autoloading, set platform constraints, and replace a…
- PHP 5 to Modern PHP: Complete Incremental Migration Guide A deep, production-minded path from PHP 5.x codebases to supported PHP 8.x: removed extensions, charset, PDO,…
- PHP Security Modernization for Legacy Applications Upgrade inherited PHP security practices: prepared statements, password hashing, sessions, CSRF, XSS escaping…
- PHP 7 to PHP 8 Migration Guide Deep guide to PHP 8.0 breaking changes that matter for PHP 7 applications, with upgrade tactics through suppo…
- Inheriting a Legacy PHP Application A first-30-days playbook for developers handed an unfamiliar PHP codebase: runtime truth, risk triage, and sa…