PSR4 Autoloading your PHP files using Composer

October 4, 2017 By - Usama Muneer | 268

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

    To follow along, you should have the understanding of the following requisites.

    Until now, you should have installed Composer and possibly created a composer.json file using composer init. If not, you can create one manually now in your project’s root.

    $ touch composer.json

    Setup PSR4 Autoloading

    As we’re concerned about setting up PSR4 autoloading here, we’ll edit the composer.json file to do so.

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

    What’s happening here?

    Here the most basic thing to understand is the vendor and application.

    • App is vendor name of your application, you can use this name while namespacing files inside of your app directory, such as:
      namespace App/User;

      Learn more about Namespaces in PHP.

    • app is your application’s directory, you want to autoload.

    Generate Vendor and AutoLoad files

    Open up your terminal and type in the following command to install autoloading files in your project.

    $ composer dump-autoload -o

    This will generate the vendor directory and autoload.php file inside of it.

    Use Case

    You just need to require once the autoload.php file once into your index.php file & you’re all set to go.

    require_once('vendor/autoload.php');

    Practical Example

    We’ll go through the practical usage of using setting up PSR-4 autoloading in your project, you’ll have to follow along.

    Winding up the Basics

    • Let’s create a new project and create an app directory inside of it.
    • Create a composer.json file and prepare it to autoload files inside your app directory. You should know how to do it now. If not, here it is
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    },
    • Run composer dump-autoload to generate the vendor directory and autoload.php
    $ composer dump-autoload -o
    • Let’s first create  a couple of classes inside of app directory
    • Create Settings.php inside app/Config
    <?php
    namespace App\Config;
    class Setings {
       //TODO
    }
    • Create Utils.php inside app/Helpers
    <?php
    namespace App\Helpers;
    class Utils {
       //TODO
    }
    • Create a new bootstrap.php file inside app directory. This is how I used to do it. You can do it directly inside your index.php file.
    <?php
    require __DIR__ .'/../vendor/autoload.php';
    • Finally, create an index.php file to test it out.
    <?php
    require 'app/bootstrap.php';
    $config = new App\Config\Settings();
    var_dump($config);

    You should be able to see the Object class returned in your terminal, like so:

    object(App\Config\Settings)#3 (0) {
    
    }

    Read more about Namespacing classes in PHP.

    Final Words

    Well that’s it. You’re all set to create new classes in your app’s directory by simply forgetting to include every single file every single time. Composer is like a lifeline in today’s world for all your PHP applications, so make sure to utilise it to its fullest while you can.
    If you’ve any suggestions or feedback, write down in the comment box below and let us know. You can also follow us on Twitter.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    What is composer?
    October 1, 2017 By - Usama Muneer

    If you're a PHP developer and has been coding up for years, you may have noticed that while developing in PHP, you have to re-invent the wheel every single time.

    Manually Installing composer is an advanced technique, however there are several reasons why a developer might prefer this reason instead of using the interactive installation routine.

    Over the years, Laravel has brought so much excitement in developing applications with its out of the box code-base

    Working with Laravel is amazing it feels magical but sometimes you get stuck in those magics Laravel is doing for you behind the scene.