Autoloading in PHP (spl_autoload_register & Composer)
In this lesson: Stop writing require for every class — autoload PHP classes with spl_autoload_register and Composer PSR-4. Code included.
Why autoloading
In a real project you may have dozens of class files. Writing a require line for each one is tedious and easy to get wrong. Autoloading lets PHP find and include a class file automatically the first time you use that class — so you write the loading logic once, not once per file.
require entirely — you require the autoloader once, then classes load themselves.The old way: many requires
Without autoloading, every file has to be included by hand:
<?php
require "User.php";
require "Product.php";
require "Order.php";
// ... and so on for every class
?>
This grows unmanageable fast, and forgetting one line causes a "class not found" error. Autoloading solves exactly this.
spl_autoload_register
Register one function that turns a class name into a file path and includes it.
<?php
spl_autoload_register(function ($class) {
require __DIR__ . "/classes/" . $class . ".php";
});
// No require for User needed — it loads automatically:
$u = new User();
?>
Now the first time PHP sees new User(), it calls your function, which includes classes/User.php. Add more classes to the folder and they just work.
Composer autoload (PSR-4)
In modern PHP, Composer handles autoloading for you using the PSR-4 standard. In composer.json:
{
"autoload": {
"psr-4": { "App\\": "src/" }
}
}
After running composer dump-autoload, you include the autoloader once:
<?php
require "vendor/autoload.php";
use App\Models\User;
$u = new User(); // loaded automatically from src/Models/User.php
?>
PSR-4 maps the App\ namespace to the src/ folder, so App\Models\User is found at src/Models/User.php — no manual requires at all.
Common mistakes
- File name not matching class name. The autoloader builds the path from the class name — they must match exactly, including case.
- Wrong folder in the path. If the class is in a different folder than your autoloader expects, it will not be found.
- Forgetting
composer dump-autoload. After adding new namespaced classes, regenerate the autoloader. - Namespace not matching PSR-4 mapping. The namespace prefix must line up with the mapped folder.
Summary
- Autoloading loads class files automatically on first use.
- It replaces many
requirelines with one setup. spl_autoload_registeris the built-in way without Composer.- Composer + PSR-4 maps a namespace to a folder and autoloads everything.
- Class name and file name (and namespace and folder) must match.
Next, organise classes with namespaces, or structure a whole app with the MVC pattern.
Frequently Asked Questions
What is autoloading in PHP?
require line for every file. It keeps large projects clean and easy to manage.What does spl_autoload_register do?
What is Composer autoload?
require "vendor/autoload.php"; once, and all your classes and installed libraries load automatically.What is PSR-4?
App\ to the src/ folder. Following it lets Composer find any class file from its namespaced name automatically.Do I still need require with autoloading?
spl_autoload_register setup or vendor/autoload.php). After that, individual class files load on their own — no per-class require.