PHP Tutorial

Composer

Dependency management and autoloading for PHP

What is Composer?

Composer is the package manager for PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. It is similar to npm for Node.js or pip for Python.
bash
composer require guzzlehttp/guzzle

composer.json

The heart of every modern PHP project is the composer.json file. It lists your dependencies and configuration.
json
{
    "require": {
        "monolog/monolog": "^2.0"
    }
}

Autoloading

Wait! Before Composer, you had to manually include or require every single file in your project. This was a nightmare.
Composer provides an Autoloader that automatically loads your classes whenever they are used.
php
require 'vendor/autoload.php';

$log = new Monolog\Logger('name'); // Just works!

Why use Composer?

  1. Dependency Resolution: If Library A needs Library B, Composer handles it automatically.
  2. Versioning: Lock your libraries to specific versions to prevent breaking changes.
  3. Standardization: Every modern PHP framework (Laravel, Symfony) uses Composer.
  4. Scripts: Run custom commands during the build process.

Summary

TermDescription
vendor/The folder where all external libraries are installed
composer.lockThe file that ensures everyone on your team has the EXACT same versions
PackagistThe official public repository for PHP packages
PSR-4The standard for mapping namespaces to file paths
requireAdding a new library to your project
installInstalling libraries from an existing lock file
Logic"Don't reinvent the wheel; use community-trusted packages"
SafetyLock files prevent "it works on my machine" bugs
ModernEssential for professional PHP development
StandardUse composer.json in every project, no matter how small
EfficiencyOnly loads the classes you actually use