composer.json deep dive
Sections tour
The composer file describes the package name, the PHP version it needs, and the libraries it depends on. Each top level key has one clear job.
Keep require strict, put dev tools in require dev, and review the whole file before sharing the project.
{
"name": "acme/demo",
"description": "Demo project",
"require": {
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
}
}
Scripts
Scripts give short names to long commands used every day. Team members then run the same checks with one memorable word.
Common scripts run tests, fix code style, and clear caches after deploy steps.
{
"scripts": {
"test": "phpunit --colors=always",
"lint": "php -l src/index.php",
"start": "php -S localhost:8000 -t public"
}
}
PSR-4 map
The autoload section maps namespace prefixes to folders. Composer then loads class files on demand without manual includes.
Match folder names to namespace parts exactly, since a small typo breaks loading on case sensitive systems.
{
"autoload": {
"psr-4": {
"Acme\\Demo\\": "src/"
}
}
}
Update safely
Update resolves fresh versions while install repeats the locked set. Run update on purpose, review the diff, and commit the lock file.
Back up the lock file first on large projects, so a bad update can be rolled back fast.
composer update
Lock file updated. Run composer install on other machines.
Next: PSR-4 autoloading
Автор статьи: Arthur Isaev