Junior — Middle
How do you use Composer to manage dependencies in a project?
sobes.tech AI
Answer from AI
Composer is a dependency manager for PHP that makes it easy to include and update libraries in a project.
Main steps for use:
- Create a
composer.jsonfile in the root of the project, where you specify the required dependencies and their versions. - Run the command
composer installto install all dependencies. - To update dependencies, use
composer update. - Include Composer's autoloader in the project via
require 'vendor/autoload.php';.
Example composer.json:
{
"require": {
"monolog/monolog": "^2.0"
}
}
This allows centralized management of libraries, simplifies updates, and ensures compatibility.