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

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 in composer.json:

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

So, how does this work?


Autoloading with Composer

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

So, first, we add the autoload key, which is an object that has the key psr-4:

"autoload": {
	"psr-4": {
    }
}

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 and app/ 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;

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

Next, we need to let Composer know where to autoload the namespace Vendor/Model from. 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 app/overrides:

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

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"]

You can also provide a fallback directory for any namespace:

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