CodeBlanks Logo Icon CodeBlanks

Autoload (PSR-4)

PHP course · Advanced · Lesson 37/92

Learn Composer's PSR-4 autoload and the use of vendor/autoload.php.

What is vendor/autoload.php?

After install, Composer produces a file named vendor/autoload.php. Calling it with require_once loads every class in the project automatically; you never write manual require or include.

The PSR-4 mapping in the autoload section binds class names to file paths. For example the App\ to src/ pair routes the App\Models\User class to the src/Models/User.php file.

composer dump-autoload

The composer dump-autoload command regenerates the vendor/autoload.php file. When you add a new class or change the autoload mapping, you run this command.

It also runs automatically after composer install or composer require; but when you only want to refresh the mapping, dump-autoload can be run on its own.

Exercise PSR-4 path resolver

Write a resolveClass(string $class, array $config): string function that mimics Composer's autoload psr-4 section. Iterate $config["autoload"]["psr-4"]; if $class starts with a prefix (check strpos against 0), strip the prefix with substr, turn the remaining \ into / with str_replace, prepend base and append .php, then return.

The $config array has autoload => psr-4 => ["App\\" => "src/"]. Print paths for App\Models\User and App\Services\Auth.

Show example solution
string { foreach ($config["autoload"]["psr-4"] as $prefix => $base) { if (strpos($class, $prefix) === 0) { $relative = substr($class, strlen($prefix)); $relative = str_replace("\\", "/", $relative); return $base . $relative . ".php"; } } return ""; }. Print with echo resolveClass("App\\Models\\User", $config) . "\n"; and echo resolveClass("App\\Services\\Auth", $config) . "\n";.
Example solution:
<?php
$config = ["autoload" => ["psr-4" => ["App\\" => "src/"]]];
function resolveClass(string $class, array $config): string {
    foreach ($config["autoload"]["psr-4"] as $prefix => $base) {
        if (strpos($class, $prefix) === 0) {
            $relative = substr($class, strlen($prefix));
            $relative = str_replace("\\", "/", $relative);
            return $base . $relative . ".php";
        }
    }
    return "";
}
echo resolveClass("App\\Models\\User", $config) . "\n";
echo resolveClass("App\\Services\\Auth", $config) . "\n";

Test yourself

  1. Match the PSR-4 mappings

    App\Models\User
    src/Models/User.php
    App\Services\Auth
    src/Services/Auth.php
    App\Controller\Home
    src/Controller/Home.php
    App\Repository\UserRepo
    src/Repository/UserRepo.php
  2. What does require_once 'vendor/autoload.php'; do?

    • It installs a new package
    • It checks version constraints
    • It deletes the vendor/ folder
    • It loads every class automatically
    Show answer

    It loads every class automatically — Correct. require_once vendor/autoload.php invokes the PSR-4 autoloader.

  3. Complete the autoload call

    <?php
    // Load every class automatically
    ____ "vendor/autoload.php";
    
    $app = new App\Application();
    echo $app->name . "\n";
    
    Show answer
    <?php
    // Load every class automatically
    require_once "vendor/autoload.php";
    
    $app = new App\Application();
    echo $app->name . "\n";
    

All lessons in Advanced