Autoloading

You've now installed a package into vendor/ — but how does your PHP code actually get access to its classes? This is where Composer's autoloading comes in, arguably the single most useful thing it does for you day to day.

The Problem: Manual Requires Don't Scale

Without Composer, using a third-party library means manually finding every class file it needs and writing a require statement for each one — in the right order, including the library's own internal dependencies. A single library can easily span dozens of files. Do this for a handful of libraries and your project's top becomes a wall of brittle require lines that break the moment a file moves.

The Solution: vendor/autoload.php

Every time Composer installs or updates packages, it generates one special file: vendor/autoload.php. Require this single file once, and it registers an autoloader that knows how to find and load any class you reference afterward — every installed package's classes, and your own project's classes too (if you've mapped them, as shown below). No more manual require chains.

A Worked Example

Suppose you've already run composer require monolog/monolog in a project. Using it needs nothing more than one require at the top of your script:

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

$log = new \Monolog\Logger('app');
$log->info('This class was autoloaded — no manual require for it!');

Notice there's no second require for the Logger class file itself — the autoloader registered by vendor/autoload.php quietly finds and loads it the moment PHP first references \Monolog\Logger. The same would apply to any other installed package's classes, and to your own classes once you've set up an autoload mapping.

PSR-4: The Modern Autoloading Standard

For your own project's classes to be autoloaded the same way, Composer needs to know how your class names map to file paths. The standard way to do this is PSR-4, a widely adopted convention where a namespace prefix corresponds to a folder, and the rest of the namespace mirrors the folder structure beneath it. So a class like Acme\MyApp\Services\Mailer living at src/Services/Mailer.php can be found automatically once Acme\MyApp\ is mapped to the src/ folder in composer.json. The full mechanics of setting up a custom PSR-4 mapping — and running composer dump-autoload to regenerate it — get their own dedicated page later in this track; for now, just know PSR-4 is the "namespace maps to folder" rule that makes autoloading your own code possible too.

Next step: Now that installed packages load automatically, learn about the file that pins their exact versions → The composer.lock File