What is Composer in PHP?

Composer is the dependency manager of PHP. It downloads the libraries (called packages) that your project needs, and keeps them updated. Packages come from a website called Packagist.

You write the needed packages in a file named composer.json. Composer installs them in the vendor folder and also creates an autoloader, so you do not need to write require for every class.

CommandWhat it does
composer initCreates a composer.json file
composer require vendor/packageInstalls a package and adds it to composer.json
composer installInstalls all packages listed in composer.lock
composer updateUpdates packages to the latest allowed version
composer dump-autoloadRebuilds the autoloader
<?php
// One line loads every installed package
require __DIR__ . "/vendor/autoload.php";
?>

Composer is used by almost all modern PHP frameworks like Laravel and Symfony. Do not upload the vendor folder to Git, only composer.json and composer.lock.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.