CodeBlanks Logo Icon CodeBlanks

What is Composer?

PHP course · Advanced · Lesson 34/92

Learn that Composer is a PHP package manager and what it provides.

What does Composer do?

Composer is a tool that manages external libraries (packages) in PHP projects. When a project needs a logging library like Monolog or an HTTP client like GuzzleHttp, you install them with a single Composer command instead of downloading files by hand and wiring up paths.

Composer also resolves relations between dependencies: if a package needs another, it installs that too automatically. This is called dependency management.

The vendor/ folder and Packagist

When Composer installs, it puts all packages into the vendor/ folder at the project root. You never touch this folder by hand; Composer manages the packages. Packages are published on a public repository called Packagist; Composer searches and downloads from there.

You do not put vendor/ under version control; when another developer runs composer install, the same packages are installed automatically.

Exercise Print the dependency list

Define a $deps array: "monolog/monolog" => "3.5", "guzzlehttp/guzzle" => "7.8", "symfony/console" => "6.4". Print each line in the form monolog/monolog: 3.5.

Show example solution
" . $ver . "\n"; }.
Example solution:
<?php
$deps = ["monolog/monolog" => "3.5", "guzzlehttp/guzzle" => "7.8", "symfony/console" => "6.4"];
foreach ($deps as $pkg => $ver) {
    echo $pkg . ": " . $ver . "\n";
}

Test yourself

  1. What is Composer's main job?

    • It manages PHP dependencies
    • It speeds up PHP code
    • It backs up databases
    • It captures browser requests
    Show answer

    It manages PHP dependencies — Correct. Composer installs and manages package dependencies.

  2. Complete the Composer command

    <?php
    // Composer command to install a package
    $cfg = ["cmd" => "____ monolog/monolog"];
    echo $cfg["cmd"] . "\n";
    
    Show answer
    <?php
    // Composer command to install a package
    $cfg = ["cmd" => "composer require monolog/monolog"];
    echo $cfg["cmd"] . "\n";
    
  3. Match the Composer concepts

    Composer
    Package manager
    vendor/
    Packages location
    Packagist
    Public repository
    require
    Adds a package

All lessons in Advanced