DEV Community

Cover image for How to Override Classes in PHP and Composer
Shahed Nasser
Shahed Nasser

Posted on • Originally published at blog.shahednasser.com

How to Override Classes in PHP and Composer

This article was originally published on my personal blog

If you're working with PHP, maybe using Laravel or some other framework, in this tutorial, we'll cover how to override classes using Composer.

This is helpful when you are using a library or package and want to override a certain functionality, but you can't really edit the code directly.


Prerequisites

This tutorial assumes you already have a project that uses Composer, thus having a composer.json file.


But First, What's PSR-4

PSR stands for PHP Standard Recommendation. PSR-4 specifies standards for namespaces, class names, etc...

For example, let's say you have the following file structure in your project:

- app
|
|_ _ _ Model
      |
      |_ _ User.php
Enter fullscreen mode Exit fullscreen mode

This is similar to the structure you'd have when using Laravel. PSR-4's standards say that the namespace should be exactly similar to the file structure. So, inside User.php the namespace should be app/Model.

However, when we're using Laravel we always capitalize app, so the namespace would be App\Model. How is that not breaking the standard?

Well, that's because of the following lines incomposer.json:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        //....
    }
}
Enter fullscreen mode Exit fullscreen mode

So, how does this work?


Autoloading with Composer

Using theautoloadkey, we can specify how to autoload certain files based on the specification, which in this case ispsr-4.

So, first, we add theautoloadkey, which is an object that has the keypsr-4:

"autoload": {
    "psr-4": {
    }
}
Enter fullscreen mode Exit fullscreen mode

Inside the psr-4 object, we'll have key-value pairs that specify how the files should be autoloaded. The key will be the namespace to be loaded, and the value is where it should be loaded from. So, in the example of Laravel, the App\ namespace is the key andapp/is the value. That means that all App\ namespaces should be loaded from the app directory.


How to Use This for Overriding Classes

Let's say we have a class Vendor\Model\User and we want to override that class. First, the class that overrides it should be in a specific directory. So, maybe you can create the path app/overrides to place your overriding classes inside. It can be any path you want it doesn't matter.

Let's say we created app/overrides/User.php. We want this class to override Vendor/Model/User. The first step is we need to make sure that app/overrides/User.php has the same namespace:

namespace Vendor/Model;
Enter fullscreen mode Exit fullscreen mode

You can then place whatever code you want inside the class.

Next, we need to let Composer know where to autoload the namespaceVendor/Modelfrom. So, the key here should beVendor/Model, and the value should be the path to the directory that has the overriding class, which in our case is app/overrides:

"autoload": {
    "psr-4": {
          "Vendor\\Model\\": "app/overrides"
    }
}
Enter fullscreen mode Exit fullscreen mode

That's it. Now, the autoloading for Vendor/Model will be from app/overrides instead of the original place and this way you can override any class you want.


Extra Options

Instead of the value being a string of the path the namespace should be loaded from, you can provide an array. This tells Composer that it should look for the classes in multiple places:

"Vendor\\Model\\": ["app/overrides", "src"]
Enter fullscreen mode Exit fullscreen mode

You can also provide a fallback directory for any namespace:

"autoload": {
    "psr-4": {
        "": "app/overides/"
    }
}
Enter fullscreen mode Exit fullscreen mode

If you would like to connect and talk more about this article or programming in general, you can find me on my Twitter account@shahednasserr

Top comments (0)