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
PHP Introduction
PHP Terminology
PHP Installation
PHP Syntax
PHP Comments
PHP exit() Function
Exercises & Assignments
Write a program to print “Hello World” using echo
Simple Tips for PHP Beginners
Experiment with Basic PHP Syntax Errors
Using phpinfo() – Display PHP Configuration & Modules
Interview Questions & Answers
What is PHP?
Who is the father of PHP?
What is the correct & common way to start and finish a PHP block of code?
What is Open Source Software?
What is the default extension of the PHP file?
What is the latest version of PHP?
What is the full form of PHP?