Laravel Macros Postmortem
Published: 2021-08-12 09:30:43

Laravel gives you ability to add methods to a class at runtime by using [Macroable (Illuminate\Support\Traits\Macroable)] (https://laravel.com/api/master/Illuminate/Support/Traits/Macroable.html) [Trait] (https://www.php.net/manual/en/language.oop5.traits.php) . You need to use [Macroable] (https://laravel.com/api/master/Illuminate/Support/Traits/Macroable.html) trait inside the class in which you want to add methods at run time.
Here is an [example] (https://laravel.com/docs/8.x/collections#extending-collections)  from the Laravel Documentation. following code adds a toUpper method to the Collection class:

!["Schema"](https://contrivesol.com/wp-content/uploads/2021/01/collectionExample.png)

## How to register your Macros?
Laravel’s Macroable Trait offers 2 ways to register your macros.
- Using <mark>Macroable::macro($name, $macro)</mark> method.
- Using <mark>Macroable::mixin($mixin, $replace)</mark> method.

## How the Magic Stuff Works?
For Magic Stuff to work, you need to have [Magic Methods] (https://www.php.net/manual/en/language.oop5.magic.php). Macros magic is based on two magic methods provided by PHP [__call()] (https://www.php.net/manual/en/language.oop5.overloading.php#object.call) and [__callStatic()] (https://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic).

How it all works behind the scene is that if you call a method on a class object which doesn’t exist and you have defined __call() magic method then control will go to this method and here you can capture request and perform any task you want, for example, may be you want to log it and throw an exception.

Laravel uses this concept very intelligently and neatly. Laravel has defined these magic methods inside Macroable trait. First Macroable trait is used inside a class where we want to add dynamic methods.
!["Schema"] 
(https://contrivesol.com/wp-content/uploads/2021/01/macroWithMagicStuff.png)

Then we register our method by calling macro static method. For example let us add a functionality in our Laravel Response Class to capitalize the value. We can do that with following piece of code. [Example] (https://laravel.com/docs/8.x/responses#response-macros) taken from Laravel Documentation
!["Schema"] 
(https://contrivesol.com/wp-content/uploads/2021/01/macroStatic.png)

Implementation of macro function is pretty straight forward. It maintains the mapping of the name of the newly added function and the callback for it.
!["Schema"] 
(https://contrivesol.com/wp-content/uploads/2021/01/registerCustomMacro.png)

After registering your macro, it is time to call it!

Once you call your registered macro, for example, Response::caps(‘laravel’), the Request Flow will be following

- Call first goes to Response Class and PHP Engine will check for the definition of called method. In our case, it will check for caps function definition in Response Class 
<mark>(Illuminate/Routing/ResponseFactory);</mark>
- If method is found, then it will be executed, else PHP Engine will check for <mark>__call()</mark> & <mark>__callStatic()</mark> Magic Methods depending whether the function is called statically or not.

-- In our case, Response Class <mark>(Illuminate/Routing/ResponseFactory)</mark> does not have caps method defined, so PHP Engine will forward the control to the either <mark>__call()</mark> & <mark>__callStatic()</mark> ( depending on the called type) defined in the Macroable Trait. Here we are first checking whether the called function is registered with our macro or not. In case of no, BadMethodCallException is thrown. In case of yes, we are binding the macro callback to the class reference and we are executing that function.
![Schema](https://contrivesol.com/wp-content/uploads/2021/01/dynamicHandle.png)

## What if I want to define multiple Macros?
Laravel again to the rescue! We can define multiple macros at once by using Macroable::mixin($mixin, $replace) method. $mixin argument is a class object which contains methods which returns callables.
![Schema](https://contrivesol.com/wp-content/uploads/2021/01/defineMultipleMacros.png)

## Scope of Macros
One important thing to note in the case of Macros are that these functions are bound to that class scope in which Macroable Trait is used in and doesn’t bound to the class where they are registered from. For example let us add whoami method in our Laravel Response Class ( *(Illuminate/Routing/ResponseFactory)* to check the scope of **$this**.
![Schema](https://contrivesol.com/wp-content/uploads/2021/01/scopeOfMacros.png)

When we call this in our test route we get a result of <mark>Illuminate\Routing\ResponseFactory</mark> and not AppService Provider.
![Schema](https://contrivesol.com/wp-content/uploads/2021/01/responseFactory.png)

The secret behind this is usage of [bindTo](https://www.php.net/manual/en/closure.bindto.php) method of [**PHP Closure Class.**](https://www.php.net/manual/en/class.closure.php) bindTo method bounds the callback with called class scope and inside your macro callback you have $this as called class scope and you can access other accessible methods and attribbutes.

## Where Macros can be defined?
You can define Macros inside Laravels’s [Service Providers](https://laravel.com/docs/8.x/providers) **App\Providers\AppServiceProvider boot()** is a good starting point. If you have many macros in your application then recommended approach or best practice would be to create your own Service Provider Class for Macros and register it in **config/app.php**

[Want to discuss more? Hit Up!](https://contrivesol.com/contact-us/)