Composer Tutorial
Composer is PHP's dependency manager. It lets you declare the packages your project needs,
downloads them (along with anything they need), and generates a single file you can require
once to autoload everything — no more manually downloading libraries or writing your own class-loading code.
Where this fits: The Slim Framework track already showed you
composer require slim/slim without explaining Composer itself in depth — this track fills that gap. If you've used npm for JavaScript or Maven for Java, Composer is PHP's equivalent.What Problem Does Composer Solve?
| Without Composer | With Composer |
|---|---|
| Manually download library files and copy them into your project | One command: composer require vendor/package |
Write your own include/require chains for every class | One require 'vendor/autoload.php'; loads everything |
| No easy way to track exact versions used | composer.lock pins exact versions for every install |
| Manually resolve which library needs which other library | Composer resolves the whole dependency tree for you |
A Quick Look Ahead
Example: A Minimal composer.json
{
"require": {
"monolog/monolog": "^3.0"
}
}
Example: Using an Installed Package
<?php
require __DIR__ . '/vendor/autoload.php';
$log = new \Monolog\Logger('app');
$log->info('Composer installed this class for us!');
Note: These are real, correct commands and code — but running them here would modify this site's own project files, so we won't execute Composer commands live on this page. Try them in your own project's terminal as you follow along.
Why Learn Composer?
| Reason | What it means for you |
|---|---|
| Industry standard | Virtually every modern PHP project — Laravel, Symfony, Slim — uses Composer |
| Huge ecosystem | Packagist.org hosts hundreds of thousands of ready-to-use PHP packages |
| Reproducible installs | composer.lock means your whole team runs the exact same versions |
| Free autoloading | No more writing manual include statements for every class |
Next steps: Head to Installation to get Composer set up on your machine.