How to Override Classes in PHP and Composer
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.
This tutorial assumes you already have a project that uses Composer, thus having a
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:
|_ _ _ 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, inside
User.phpthe namespace should be
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 in
So, how does this work?
Autoloading with Composer
autoloadkey, we can specify how to autoload certain files based on the specification, which in this case is
So, first, we add the
autoloadkey, which is an object that has the key
psr-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, the
App\namespace is the key and
app/is the value. That means that all
App\namespaces should be loaded from the
How to Use This for Overriding Classes
Let’s say we have a class
Vendor\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 path
app/overridesto 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.phphas the same namespace:
You can then place whatever code you want inside the class.
Next, we need to let Composer know where to autoload the namespace
Vendor/Modelfrom. So, the key here should be
Vendor/Model, and the value should be the path to the directory that has the overriding class, which in our case is
That’s it. Now, the autoloading for
Vendor/Modelwill be from
app/overridesinstead of the original place, and this way you can override any class you want.
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:
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