Composer, Packagist & Autoloading
The tool every real PHP project uses to install packages and load classes automatically — Composer, the Packagist library, and autoloading.
What you will learn
- Understand what Composer and Packagist are
- Install a package and use it
- Let Composer autoload your classes so you never write include again
The problem Composer solves
So far, every time you wanted code from another file you wrote include or require by hand. On a real project that is dozens of files, and you would also need to find, download and update outside libraries (for email, PDFs, payments) manually. Composer automates all of this. It is to PHP what npm is to JavaScript — the universal dependency manager. No modern PHP project, including Laravel, works without it.
Two words to know. A dependency is outside code your project relies on (a library someone else wrote). Packagist is the giant public catalogue of PHP packages that Composer downloads from — like an app store for code.
How a Composer project works
Composer revolves around one file, composer.json, which lists what your project needs. The flow is always the same:
- You install Composer once on your machine (from getcomposer.org).
- In your project folder you run
composer require vendor/packageto add a library. - Composer downloads it into a
vendor/folder and records it incomposer.json. - You add one line —
require "vendor/autoload.php"— to your app, and every installed package (and your own classes) becomes available automatically. - Anyone else can run
composer installto recreate the exact samevendor/folder from yourcomposer.json.
Here is installing a real, popular package — a fast unique-ID generator — from the terminal:
# in your project folder
composer require ramsey/uuid
# Composer downloads it into vendor/ and updates composer.jsonNote: Output (in the terminal):
Using version ^4.7 for ramsey/uuid
./composer.json has been updated
Generating autoload files
Composer fetched the library and everything it needs, wrote it into vendor/, and regenerated the autoloader. You did not download or unzip anything by hand.
Now you use that package in your code. The single require of Composer’s autoloader makes the library’s classes available with no per-file includes:
<?php
require "vendor/autoload.php"; // the one include you still need
use Ramsey\Uuid\Uuid;
$id = Uuid::uuid4();
echo "New ID: " . $id;
?>require "vendor/autoload.php" loads Composer’s autoloader — the one and only include in a Composer project. The use line brings the Uuid class into scope (you will learn use properly in the next lesson). Uuid::uuid4() then generates a random unique identifier — code you never had to write or download manually.
Note: Output (in the browser):
New ID: 1ec9b2e1-7f3a-4c2d-9b8a-2f5e1d3c4a6b
(The exact ID is random each time.) Notice you never wrote include "Uuid.php" — autoloading found the class for you the moment you used it. That is the magic.
Autoloading your OWN classes
Autoloading means PHP loads a class file automatically the first time you use the class — no manual require. You tell Composer where your classes live by adding an autoload section to composer.json, then run composer dump-autoload.
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}This says "any class whose name starts with App\ lives in the src/ folder". So a class App\Models\User is expected at src/Models/User.php. After adding this and running composer dump-autoload, you can use new App\Models\User() anywhere and Composer finds and loads the file for you — no require at all.
Note: There is no page output here — this is configuration. The payoff is huge: across a 200-file project you write zero include statements. This psr-4 rule is the standard you will see in every modern PHP and Laravel project; the next lesson explains the namespaces it relies on.
Q. What is Composer used for in PHP?
✍️ Practice
- Install Composer and run
composer require ramsey/uuidin a test folder. - Add the autoloader line and generate a UUID on a page.
🏠 Homework
- Set up a project with a
psr-4autoload mappingApp\tosrc/, add one class, and use it without any require.