Registering Autoloaders
Set up spl_autoload_register and Composer-based autoloading.
Registering Autoloaders is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
spl_autoload_register
spl_autoload_register() registers a callback PHP calls whenever an undefined class is used. Multiple autoloaders run in registration order.
Basic Custom Autoloader
A minimal PSR-4-style autoloader written by hand:
<?php
spl_autoload_register(function (string $class): void {
$prefix = 'App\\';
$baseDir = __DIR__.'/src/';
if (!str_starts_with($class, $prefix)) return;
$rel = substr($class, strlen($prefix));
$file = $baseDir.str_replace('\\', '/', $rel).'.php';
if (file_exists($file)) require $file;
});Multiple Autoloaders
Register one autoloader per namespace prefix so each handles only its own classes.
<?php
spl_autoload_register(fn($c) => loadFromSrc($c));
spl_autoload_register(fn($c) => loadFromLib($c));Composer's autoload.php
In practice you almost never write your own autoloader. Composer generates one that covers all packages plus your PSR-4 mappings.
<?php
require __DIR__.'/vendor/autoload.php';dump-autoload
After adding new classes or changing composer.json autoload settings, regenerate the autoloader: composer dump-autoload.
Optimised Autoloader
composer dump-autoload --optimize builds a static classmap — eliminates filesystem scans at runtime. Recommended for production.
Autoloading Files (global functions)
Use the files key in composer.json for helper files with global functions. They are always included.
// composer.json
{
"autoload": {
"files": ["src/helpers.php"]
}
}Autoloader Execution Order
Autoloaders are called in registration order. If the first cannot find the class, it returns without error and PHP tries the next one.
Debugging Autoloading Issues
Common causes of "Class not found": missing dump-autoload, wrong case in file/class name, or namespace prefix mismatch in composer.json.
PSR-4 vs classmap Performance
PSR-4 does filesystem stat calls. classmap does a hash-map lookup — faster but requires regeneration on every new class.
Testing Autoloading
Verify your autoloader:
<?php
require 'vendor/autoload.php';
$obj = new \App\Services\EmailService();
echo get_class($obj); // App\Services\EmailServiceSummary
spl_autoload_register underpins all PHP autoloading. Composer handles the heavy lifting — just run dump-autoload when you add new classes.
Quick Check
Which command regenerates the Composer autoloader?
Frequently asked questions
Is the “Registering Autoloaders” lesson free?
Yes — the full text of “Registering Autoloaders” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Registering Autoloaders”?
Set up spl_autoload_register and Composer-based autoloading. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Registering Autoloaders” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring and Using Namespaces
- Nested Namespaces and Aliases
- PSR-4 Autoloading Standard
- Registering Autoloaders