You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
The PHP Macro package offers closure (anonymous function) based setter dependencyinjection by providing a trait which can be included into any class.
composer req aimeos/macro
This package is for application, framework and library developers who want toallow customizing the behavior of their code by their users.
Why macros
In applications, frameworks or libraries which are build for customization it’snecessary to allow overwriting existing functionality to be able customize itsbehavior. This is where macros are very handy because they can add custom codeusing closures.
With the PHP Macro package, you can also allow users to overwrite methods inbase classes without forcing your users to extend these classes. The PHP Macropackage usesNO reflection or other hacks, justpure PHP methods.
There are some pros and cons when compared to class based depencency injection:
Pro:
Less code to write and much easier to implement for simple stuff
Custom closures can be inherited and overwritten like class methods
Con:
Limited static code analysis possibilities
Anonymous function can not be forced to implement an interface
Thus, it's not a replacement for class based depencency injection but a lightweightaddition for small extension points where full-blown dependency injection usingclasses implementing interfaces are too much work.
Allow customization
The result of existing methods can be modified if the original method checksfor an existing macro and use that instead its own implementation:
The macro can use the property as input for creating the returned value.
Use inherited macros
The PHP macro package also allows to inherit macros from parent classes. Then,they can access class properties of the child class just like regular classmethods:
// original codeclass A{useAimeos\Macro\Macroable;private$name ='A';};class Bextends A{private$name ='B';};
Macros added to the parent class will be available in child classes too:
ClassB extends from classA but provides a different$name property. Themacro inherited from classA will now use the property of classB.
Overwrite inherited macros
It's also possible to overwrite macros inherited from parent classes as it'spossible with regular class methods:
// original codeclass A{useAimeos\Macro\Macroable;publicfunctiondo() {returnstatic::macro('concat' )( [1,2,3] ); }};class Bextends A {};class Cextends A {};
Now you can add macros to the parent class and one of the child classes:
This enables you to add special handling for single classes even if all otherclasses still use the macro added to classA.
Overwrite protected methods
Base classes often offer a set of methods that are used by the child classes.In PHP, replacing the methods of a base class is impossible and thus, you haveto overwrite each child class with your own implementation.
To avoid that, the original method can use thecall() method instead of callingthe method of the parent class directly: