Understanding PHP __autoload() and PSR-4 Autoloading
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! 🚀
🤖 What is Autoloading in PHP?
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! 👏
🧓 __autoload() – The Old Way
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.
🔁 spl_autoload_register() – The Modern Way
The modern replacement is spl_autoload_register().
You can register multiple autoload functions and even anonymous functions.
Example:
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! 🎉
📦 Composer and PSR-4 Autoloading
When working with modern PHP (Laravel, Symfony, etc.), you should use Composer’s autoloading, which follows the PSR-4 standard.
🤔 What is PSR-4?
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 Structure Example:
project/
├── composer.json
├── src/
│ └── Models/
│ └── User.php
🔧 composer.json
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
🧠 Steps to Use PSR-4:
Create
composer.jsonwith PSR-4 config.Run:
composer dump-autoloadUse your classes with namespaces!
💡 Example:
// 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.
🧠 Autoloading Multiple Directories
You can map more than one directory too:
"autoload": {
"psr-4": {
"App\\": "src/",
"Lib\\": "lib/"
}
}
Then files under lib/ will follow the Lib\ namespace.
💡 Best Practices
- ✅ Use Composer with PSR-4 for any modern PHP project.
- ✅ Stick to autoloading and avoid manual includes.
- ✅ Always run
composer dump-autoloadafter adding new classes. - ❌ Don’t mix autoload with manual
requireunless necessary. - ✅ Use namespaces properly for better scalability.
🧾 Summary
| Technique | Status | Use Case |
|---|---|---|
__autoload() |
❌ Removed | Legacy code only |
spl_autoload_register() |
✅ Good | Simple custom projects |
| Composer + PSR-4 | ✅ Best | Modern PHP apps & frameworks |
🗣️ Final Thoughts
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.
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.