PSR-4 Autoloading Your PHP Files using Composer Tutorial

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.

In this article, we’ll go through a brief introduction on setting up PSR-4 Autoloading using Composer within your project.

Visit PSR-4 and you will see a fully qualified class name has the following form:

psr-4-composer-autoloading

Look the fully qualified class name MUST have a top-level namespace name, also known as a “vendor namespace”. 

Setup PSR-4 

For setup PSR-4 , if you are running windows on your pc then visit 

https://getcomposer.org/download/ 

and download and install it. If you are using MAC or Linux then open your command and run one  after another to setup composer.. 

Step 1: php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 
Step 2: ls | grep composer Step 
Step 3: php composer-setup.php Step 
Step 4: php composer.phar -v

Move it globally

 mv composer.phar /usr/local/bin/composer

& check again

composer -v

Setup oh-unit on composer

composer require --dev phpunit/phpunit 

Now our composer setup is done. Now let's start our example tutorial. So make a directory structure like below.

psr-4-autoloading-example

Without vendor directory create those folder in your project and create composer.json file and open it and paste the following code.

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

Here Codechief is vendor name of your application, you can use this name while namespacing files inside of your app directory, such as:

namespace Codechief\Model;

Look according to PSR-4 mechanism Codechief our Namespace name and Filter our SubNamespace name and then class name. Look again again below pic to understand.

psr-4-composer-autoloading

Hope you will understand it. Now go to your project root diretcory and open command window and run this following command to install psr-4 to your project.

Setup composer on your project

Now for installing composer on your directory run follwoing command

composer dump-autoload -o

After running this command you will get composer on your project. Now i wanna show you , how you can use it in your project. 

Exmaple PSR-4 : How to use

After completing this structure 

open your app/Codechief/Model/User.php file and paste the following code.

app/Codechief/Model/User.php

 
namespace Codechief\Model;

class User {
    
    public function __construct(){
       
    }

    public function Display(){
        echo "I am Codechief";
    }
}

Look we no need to php require() or include() to add file in this file. fpor composer autoloading we can use namespace to add file. Hope you will understand. Now open your app/Codechief/Filters/Filter.php and paste the following code

your app/Codechief/Filters/Filter.php

namespace Codechief\Filters;
use Codechief\Model\User; 

class Filter {
   
    public function __construct(){
       
    }

    public function GetDataFromUserClass(User $class){
        return $class->Display();
    }
}

Look here we use User class data that mean Display() methos for usinig it top of the file like

use Codechief\Model\User; 

Hope you got it. Now open your init.php file.You just need to require once the autoload.php file once into your init.php file & you’re all set to go.

require_once __DIR__ . '/../vendor/autoload.php';

Now open your index.php file and paste the following code.

require_once 'app/init.php';

use Codecouese\Filters\Filter as Filter;
use Codecouese\Model\User as User;

$filter = new Filter();
echo $filter->GetDataFromUserClass(new User());

Look here we include init.php file and then use all file whatever you want. It's just an example , hope you have learned how to use psr-4 in your project and autoloading file. look GetDataFromUserClass(new User()) take a parameter which is class object. So we just created an object and and then access this method and pass the User class as a parameter. 

Read Also : Your First Time with Git and Github

OUTPUT: 

I am Codechief

Hope it will help you. if you like this tutorial please gives a thumbsup don't forget to share.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website