Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a PSR-4 autoloader for my project?

I am creating a PHP project and want to implement PSR-4 autoloading.

I don't know which files I need to create in the vendor directory to implement autoloading for class files.

like image 359
Kshitij Verma Avatar asked Mar 29 '20 18:03

Kshitij Verma


People also ask

What's the purpose of PSR-4 autoloading?

Overview. This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

What does autoloader do in PHP?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. The class Views would be placed in Views.

What is PSR 4 autoloader example?

PHP PSR-4: Autoloader. Example. PSR-4 is an accepted recommendation that outlines the standard for autoloading classes via filenames. This recommendation is recommended as the alternative to the earlier (and now deprecated) PSR-0. The fully qualified class name should match the following requirement:

How to load classes in composer using PSR-4?

Composer package manager supports PSR-4 which means, if you follow the standard, you can load your classes in your project automatically using Composer's vendor autoloader. Now in your code you can do the following:

What is the use of psr-0?

PSR describes a specification for autoloading classes from file paths. It is fully inter-operable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

What does PSR mean in composer dump-autoload?

However, if you run the composer dump-autoload command again, the index.php file will work properly. PSR stands for PHP Standard Recommendation. PSR is a PHP specification published by the PHP Framework Interop Group or PHP-FIG.


Video Answer


1 Answers

If you are using composer, you do not create the autoloader but let composer do its job and create it for you.

The only thing you need to do is create the appropriate configuration on composer.json and execute composer dump-autoload.

E.g.:

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

By doing the above, if you have a file structure like this

├── src/
│   ├── Controller/
│   ├── Model/
│   ├── View/
│   └── Kernel.php
├── public/
│   └── index.php
└── vendor/

After executing composer dump-autoload the autoloader will be generated on vendor/autoload.php.

All your classes should be nested inside the App namespace, and you should put only one class per file.

E.g.:

<?php /* src/Controller/Home.php */

namespace App\Controller;

class Home { /* implementation */ }

And you need only to include the autoloader in your entry-point script (e.g. index.php).

<?php

require '../vendor/autoload.php';

Which will allow you to simply load your classes directly from anywhere after this point, like this:

use App\Controller\Home;

$homeController = new Home();

This is explained at the docs, here.

like image 88
yivi Avatar answered Oct 19 '22 19:10

yivi