Composer 101: Managing PHP Dependencies the Right Way

Before Composer, managing PHP dependencies meant manually downloading libraries and dealing with version conflicts by hand. Composer brought PHP in line with npm and Cargo — a proper dependency manager with a lockfile.

Getting Started

composer init
composer require monolog/monolog
composer require --dev phpunit/phpunit

# composer.lock pins exact versions - always commit it
composer install   # installs from the lockfile, reproducibly

Autoloading

require __DIR__ . '/vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$log = new Logger('app');
$log->pushHandler(new StreamHandler('app.log'));
$log->warning('Low disk space');

The generated vendor/autoload.php handles class loading automatically via PSR-4, so you never write a manual require for a library class again.