Kritim Yantra
May 19, 2025
In any PHP project, especially as it grows, organizing and managing your classes can get complex. You don’t want to manually include every PHP file using require
or include
, right?
That’s where autoloading comes in!
In this blog, you’ll learn:
✅ What is autoloading in PHP
✅ How __autoload()
works (and why it’s deprecated)
✅ The rise of spl_autoload_register()
✅ What is PSR-4 Autoloading
✅ How Composer handles autoloading
✅ Hands-on example with custom autoloading and Composer
✅ Best practices
Let’s jump in! 🚀
Autoloading means automatically including the necessary class file when a class is used.
Instead of this:
require 'classes/User.php';
require 'classes/Admin.php';
$user = new User();
You can just write:
$user = new User(); // PHP will automatically find and load the class
That’s autoloading! 👏
In PHP 5, autoloading was introduced via a magic function called __autoload()
:
function __autoload($class) {
require "classes/$class.php";
}
Then you could do:
$obj = new MyClass();
✅ Simple.
❌ BUT — only one __autoload
function is allowed per script. No flexibility.
That’s why it was deprecated in PHP 7.2 and removed in PHP 8.0.
The modern replacement is spl_autoload_register()
.
You can register multiple autoload functions and even anonymous functions.
spl_autoload_register(function ($class) {
require "classes/$class.php";
});
Now, if you create a file classes/User.php
:
// classes/User.php
class User {
public function greet() {
echo "Hello User!";
}
}
And in your main file:
$user = new User();
$user->greet(); // Hello User!
No manual require
needed! 🎉
When working with modern PHP (Laravel, Symfony, etc.), you should use Composer’s autoloading, which follows the PSR-4 standard.
PSR-4 is a PHP-FIG standard that maps namespaces to file paths.
Namespace
App\Models\User
Class file path:src/Models/User.php
project/
├── composer.json
├── src/
│ └── Models/
│ └── User.php
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
Create composer.json
with PSR-4 config.
Run:
composer dump-autoload
Use your classes with namespaces!
// src/Models/User.php
namespace App\Models;
class User {
public function sayHi() {
echo "Hi from User!";
}
}
// index.php
require 'vendor/autoload.php';
use App\Models\User;
$user = new User();
$user->sayHi(); // Hi from User!
Boom! ✅ Clean, autoloaded, organized.
You can map more than one directory too:
"autoload": {
"psr-4": {
"App\\": "src/",
"Lib\\": "lib/"
}
}
Then files under lib/
will follow the Lib\
namespace.
composer dump-autoload
after adding new classes.require
unless necessary.Technique | Status | Use Case |
---|---|---|
__autoload() |
❌ Removed | Legacy code only |
spl_autoload_register() |
✅ Good | Simple custom projects |
Composer + PSR-4 | ✅ Best | Modern PHP apps & frameworks |
Autoloading helps PHP developers write cleaner, modular, scalable code. With Composer and PSR-4, you can focus on building features — not including files manually.
Whether you're building a micro-project or a Laravel application, proper autoloading will save you time and reduce errors.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google