Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 22

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.

💡 Autoloading does not remove 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 require lines with one setup.
  • spl_autoload_register is 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?
Autoloading means PHP loads a class file automatically the first time you use the class, so you do not have to write a require line for every file. It keeps large projects clean and easy to manage.
What does spl_autoload_register do?
It registers a function that PHP calls whenever you use a class that is not loaded yet. Your function works out the file path from the class name and includes it. This is the built-in way to autoload without Composer.
What is Composer autoload?
Composer is PHP's package manager. It generates an autoloader for you based on the PSR-4 standard. You just add require "vendor/autoload.php"; once, and all your classes and installed libraries load automatically.
What is PSR-4?
PSR-4 is a standard that maps a namespace to a folder — for example 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?
You still require the autoloader itself once (either your spl_autoload_register setup or vendor/autoload.php). After that, individual class files load on their own — no per-class require.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicTraits Your progress: 22 / 50 (44%) · Lesson 23 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free