0Pricing
PHP Academy · Lesson

PSR-4 Autoloading Standard

Understand how PSR-4 maps namespace segments to directory paths.

PSR-4 Autoloading Standard is a free PHP Academy lesson on CoddyKit — lesson 3 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.

What is Autoloading?

Autoloading means PHP automatically loads a class file the first time you use that class — no manual require needed.

Before Autoloading

Without autoloading, every file began with dozens of require_once calls — fragile and hard to maintain.

PSR-4 Rule

PSR-4 maps a namespace prefix to a base directory. Given prefix App\src/: App\Models\User loads from src/Models/User.php.

The Mapping Formula

Strip the prefix, replace remaining backslashes with directory separators, append .php.

App\Services\Payment\Stripesrc/Services/Payment/Stripe.php

composer.json autoload Section

Define PSR-4 mappings in composer.json. Run composer dump-autoload after changes.

// composer.json
{
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

Multiple Namespace Prefixes

A project can have multiple PSR-4 mappings — useful for separating test namespaces.

// composer.json
{
  "autoload":     { "psr-4": { "App\\": "src/" } },
  "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }
}

Class Name = File Name

The class name must exactly match the file name (case-sensitive on Linux). class UserService must live in UserService.php.

One Class Per File

PSR-4 requires exactly one class, interface, or trait per file. Multiple definitions break autoloading.

PSR-4 vs PSR-0

PSR-0 (deprecated) used underscores in class names as directory separators. PSR-4 is cleaner and is the current standard.

The Generated Autoloader

After composer dump-autoload, Composer writes vendor/autoload.php. Require it once at the entry point.

<?php
require __DIR__.'/vendor/autoload.php';

$user = new App\Models\User(); // autoloads automatically

Classmap Fallback

Composer also generates a classmap for files that do not follow PSR-4. Useful for legacy code.

Practical Directory Layout

src/
  Models/User.php        → App\Models\User
  Http/Controllers/
    UserController.php   → App\Http\Controllers\UserController

Quick Check

Where does PSR-4 find App\Services\Mailer if prefix App\\ maps to src/?

Frequently asked questions

Is the “PSR-4 Autoloading Standard” lesson free?

Yes — the full text of “PSR-4 Autoloading Standard” 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 “PSR-4 Autoloading Standard”?

Understand how PSR-4 maps namespace segments to directory paths. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “PSR-4 Autoloading Standard” 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

  1. Declaring and Using Namespaces
  2. Nested Namespaces and Aliases
  3. PSR-4 Autoloading Standard
  4. Registering Autoloaders
← Back to PHP Academy