DEV Community

vimuth
vimuth

Posted on

PSR-4 autoloading in Laravel

What is Autoloading?

Autoloading in PHP is a mechanism that automatically loads the classes, interfaces, or traits you need when you use them, without the need to manually include or require their files. This helps keep your code clean and organized, especially in large projects.

Example Without Autoloading

1.Define Classes
Let's say you have two classes in different files:

// File: app/Services/ExampleService.php
namespace App\Services;

class ExampleService {
    public function sayHello() {
        return 'Hello, World!';
    }
}

// File: app/Helpers/Helper.php
namespace App\Helpers;

class Helper {
    public function greet() {
        return 'Greetings!';
    }
}
Enter fullscreen mode Exit fullscreen mode

2.Include Classes Manually
In your main script, you need to manually include these files:

// File: index.php

require 'app/Services/ExampleService.php';
require 'app/Helpers/Helper.php';

use App\Services\ExampleService;
use App\Helpers\Helper;

$exampleService = new ExampleService();
echo $exampleService->sayHello(); // Outputs: Hello, World!

$helper = new Helper();
echo $helper->greet(); // Outputs: Greetings!
Enter fullscreen mode Exit fullscreen mode

Errors on this approach

  • You need to write a require statement for every single class file you use.
  • It’s easy to forget to include a file, leading to errors.
  • If we add require statement multiple places for same file it can lead to errors.

For this last error we can use 'require_once' but it is slower and first issue still exists.

PSR-4 Autoloading with Composer

To avoid those issues laravel uses PSR-4 autoloading with composer. It is really simple.

1.Define Classes
The class definitions remain the same as above:

// File: app/Services/ExampleService.php
namespace App\Services;

class ExampleService {
    public function sayHello() {
        return 'Hello, World!';
    }
}

// File: app/Helpers/Helper.php
namespace App\Helpers;

class Helper {
    public function greet() {
        return 'Greetings!';
    }
}
Enter fullscreen mode Exit fullscreen mode

2.Configure Composer
Add the autoload section in your composer.json file:

{
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

3.Run the command to generate the autoload files:

composer dump-autoload
Enter fullscreen mode Exit fullscreen mode

4.Use Classes Without Manual Includes
Now, in your main script, you can use the classes without manually including their files:

// File: index.php

require 'vendor/autoload.php'; // Composer's autoload file

use App\Services\ExampleService;
use App\Helpers\Helper;

$exampleService = new ExampleService();
echo $exampleService->sayHello(); // Outputs: Hello, World!

$helper = new Helper();
echo $helper->greet(); // Outputs: Greetings!

Enter fullscreen mode Exit fullscreen mode

That is it. Composer does the rest. Cool right?

Top comments (0)