Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Nicola Malizia:
Understanding The Laravel Macroable Trait
Feb 14, 2017 @ 16:53:45

In this post to his site Nicola Malizia briefly helps you understand the Laravel "macroable" trait - what it is and how to can be used in your own code.

If you check the Laravel codebase I’m sure that you can observe that Laravel makes use of traits.

There is one trait in the source code that pulls my attention. I’m talking about the Macroable trait. In case you don’t know, you can define custom responses using macros.

He includes an example of extending the default Response class with a "caps" macro and how it would then be used in the resulting object. He talks about how traits work in PHP OOP code and how they can be used to "inherit" functionality into a class. The "macroable" trait then uses the __call magic method to do its thing, looking for macros that match the function being called.

tagged: macro macroable trait laravel example tutorial

Link: https://unnikked.ga/understanding-the-laravel-macroable-trait-dab051f09172#.g5xrqlk5s


Trending Topics: