How to Override Classes in PHP and Composer

Shahed Nasser
3 min readApr 25, 2021

--

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.jsonfile.

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

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, insideUser.phpthe namespace should beapp/Model.

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

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

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

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": {
}
}

Inside thepsr-4object, 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, theApp\namespace is the key andapp/is the value. That means that allApp\namespaces should be loaded from theappdirectory.

How to Use This for Overriding Classes

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

Let’s say we createdapp/overrides/User.php. We want this class to overrideVendor/Model/User. The first step is we need to make sure thatapp/overrides/User.phphas the same namespace:

namespace Vendor/Model;

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 isapp/overrides:

"autoload": {
"psr-4": {
"Vendor\\Model\\": "app/overrides"
}
}

That’s it. Now, the autoloading forVendor/Modelwill be fromapp/overridesinstead 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"]

You can also provide a fallback directory for any namespace:

"autoload": {
"psr-4": {
"": "app/overides/"
}
}

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

--

--

No responses yet