Composer and autoloading
Composer is the dependency manager of PHP. It reads one file, installs the libraries the project names and writes the autoloader that loads every class on demand.
What Composer does
Composer is the PHP dependency manager: it reads the composer.json file, fetches the listed packages into the vendor folder, and generates an autoloader for all of them. Fetch it from getcomposer.org and run the installer once per machine.
The file composer.json:
{ "name": "acme/shop", "version": "1.0.0" }
The script that reads it, read.php:
<?php $json = file_get_contents(__DIR__ . "/composer.json"); $data = json_decode($json, true); echo $data["name"] ?? "no name", PHP_EOL;
php read.php
acme/shop
file_get_contents() reads the whole file and json_decode() turns JSON text into an array, which is how a script can read the project name and the dependency lists of its own Composer file.
PSR-4 autoloading
PSR-4 autoloading maps a namespace prefix to a folder, so the engine loads each class file on first use with no manual require per class. A tiny autoloader shows the idea: turn the class name into a path and require it when the file exists.
The class file src/Message.php:
<?php class Message { public function text(): string { return "loaded by the autoloader"; } }
The page that never requires the class by hand, index.php:
<?php spl_autoload_register(function (string $class): void { $path = __DIR__ . "/src/" . $class . ".php"; if (is_file($path)) { require($path); } }); $message = new Message(); echo $message->text(), PHP_EOL;
php index.php
loaded by the autoloader
spl_autoload_register() stores the function, and PHP calls it with the class name as soon as new Message meets a class it does not know yet. The loader builds the path, checks it with is_file() and requires the file.
require vendor/autoload.php
Every Composer project shares one entry file that knows all installed packages and your own namespace map. Requiring it first in the front controller makes every class available to the whole request.
The front controller, index.php:
<?php require(__DIR__ . "/vendor/autoload.php"); $app = new App\Kernel(); $app->run();
composer dump-autoload
php index.php
Generating autoload files
Generated autoload files
running
The first command reads the map from composer.json and writes vendor/autoload.php; the second one runs the page, which loads App\Kernel without a single require of that class. The class it creates is the src/Kernel.php of the next section.
Your first autoloaded class
Files under src/ mirror the App namespace: the class App\Kernel lives in src/Kernel.php. After adding the map to composer.json, regenerate it once and never require the class by hand again.
The file src/Kernel.php:
<?php namespace App; class Kernel { public function run(): void { echo "running", PHP_EOL; } }
The map that turns the namespace into that path is the one the command above reads:
{ "autoload": { "psr-4": { "App\": "src/" } } }
The prefix App\ points at the folder src/, so the rest of the class name is the rest of the path: Kernel is src/Kernel.php.
Exercises
- Add a "description" key to composer.json and print it with the read script. What happens when the key is missing?
- Give Message a namespace, add a PSR-4 map for it and regenerate the autoloader. Which path does the class file take?
- Call new Missing() with the tiny autoloader registered. What does PHP report, and why does the is_file check not stop it?
- Delete vendor/ and run php index.php. Which error appears, and which command writes the folder again?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which file does Composer read to learn the packages of a project?
Answer
composer.jsonThe composer.json file names the packages, the PHP version and the autoload map; the lock file pins the versions it resolved.
-
What does the PSR-4 map in
composer.jsondo?Answer
It maps a namespace prefix to a folder for autoloading
The map tells the generated autoloader where the files of a namespace live, so a class loads on first use.
-
What does
vendor/autoload.phpcontain?Answer
The generated loader that knows the packages and the project namespace map
Composer writes this entry file from composer.json and the lock file; requiring it registers every autoloader.
-
When does an autoloader run?
Answer
When the code uses a class that is not declared yet
The engine asks the registered loaders only for a class it cannot find, which is what makes loading lazy.
-
What does this program print?
<?php spl_autoload_register(function (string $class): void { echo "loader asked for " . $class; }); class_exists("Missing");Answer
loader asked for Missing
class_exists asks the autoloaders for the unknown class, and the registered function prints its name.
-
What does this program print?
<?php file_put_contents(__DIR__ . "/Widget.php", "<?php class Widget { public function name(): string { return 'widget'; } }"); spl_autoload_register(function (string $class): void { $path = __DIR__ . "/" . $class . ".php"; if (is_file($path)) { require($path); } }); $w = new Widget(); echo $w->name();Answer
widget
The loader finds Widget.php, requires it, and the method returns the word widget.
-
What does this program print?
<?php echo json_decode('{"name":"acme/shop"}', true)["name"];Answer
acme/shop
json_decode with true gives an array, and the key name holds the string acme/shop.
-
Which command writes
vendor/autoload.phpfrom the map incomposer.json?Answer
composer dump-autoloaddump-autoload regenerates the loader files from the current composer.json without installing anything.
-
Which file does the map
"App\\": "src/"make the classApp\Kernelload?Answer
src/Kernel.phpThe prefix App\ is replaced by the folder src/, and the rest of the class name Kernel becomes Kernel.php.
-
What does
require(__DIR__ . "/vendor/autoload.php");add to a page?Answer
Every package class, without a require for each one
The generated file registers the autoloaders, so any installed or mapped class loads when the page first uses it.
The code on this page was run with PHP 8.5.13 (php -v) and Composer 2.10.3 in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev