← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 7 of 24

PHP Composer & Packages

Composer is like an app store for PHP code. You list the helper libraries you want, and it downloads them and keeps them up to date for you.
Syntax
php
// composer.json
{
    "require": {
        "vendor/package": "^1.0"
    }
}

// composer install
require "vendor/autoload.php";

What is Composer?

Composer is PHP's standard package manager, letting you declare which third-party libraries your project depends on and automatically download the right versions instead of copying code in by hand.

Example: What is Composer?

php
<?php
// Print "Composer downloads the exact dependency versions your project declares." to the output
echo "Composer downloads the exact dependency versions your project declares.";
?>

Creating composer.json

composer.json sits in your project root and lists each dependency along with a version constraint, acting as the single source of truth for what your project needs to run.

Example: Creating composer.json

php
<?php
$composerJson = [
    "require" => [
        "monolog/monolog" => "^2.0"
    ]
];
// Print `json_encode($composerJson, JSON_PRETTY_PRINT)` to the output
echo json_encode($composerJson, JSON_PRETTY_PRINT);
?>

Installing Packages

Running composer install reads composer.json, resolves compatible versions for every dependency, and downloads them all into a vendor/ folder that your code then requires from.

Example: Installing Packages

php
<?php
// Run in terminal: composer install
echo "Downloads dependencies into vendor/ based on composer.json";
?>

Loading Composer's Autoloader

Including vendor/autoload.php at the top of your entry script wires up Composer's autoloader, which means every installed package's classes become available without a single manual require statement.

Example: Loading Composer's Autoloader

php
<?php
// require __DIR__ . '/vendor/autoload.php';
echo "One require statement makes every installed package's classes available";
?>

Managing Package Versions

Composer follows semantic versioning conventions: a caret (^1.2.0) allows any non-breaking update, while a tilde (~1.2.0) restricts updates to patch-level releases only, giving you fine control over how aggressively dependencies update.

Example: Managing Package Versions

php
<?php
$constraints = [
    "^1.2.0" => "any non-breaking update",
    "~1.2.0" => "patch-level updates only",
];
// Loop over `$constraints`, binding each item to `$range => $meaning`
foreach ($constraints as $range => $meaning) {
    // Print "$range: $meaning\n" to the output
    echo "$range: $meaning\n";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting require 'vendor/autoload.php';, so installed packages cannot be found.
  2. Committing the vendor folder to version control instead of the composer.json and composer.lock files.
  3. Editing composer.lock by hand, which can cause version mismatches.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.