aimeos/macro
aimeos/macro is a lightweight PHP macro/extension library that lets you add methods at runtime and extend objects and classes without inheritance. Handy for creating fluent APIs, plugin-style features, and flexible customization in Laravel or any PHP project.
Start by requiring the package via Composer: composer require aimeos/macro. The core concept is enabling dynamic method injection on any class or object using closures. The package provides a Macroable trait—attach it to any class to gain macro capabilities. First use case: define custom string manipulation methods on the Str class or your own domain-specific classes without subclassing.
use Aimeos\Macro\Macroable;
class TextProcessor
{
use Macroable;
}
TextProcessor::macro('capitalizeWords', function ($str) {
return ucwords($str);
});
$processor = new TextProcessor();
echo $processor->capitalizeWords('hello world'); // "Hello World"
Check the src/ folder for the Macroable trait implementation and the tests/ for real-world examples.
ClassName::macro('name', $closure) during application boot (e.g., in AppServiceProvider).instance()->macro()—useful for runtime-specific customization.App\Macros\DateTimeMacros).For Laravel apps, bind macros inside service providers or use auto-discovery via MacroServiceProvider pattern. Keep macros short, focused, and side-effect-free.
myapp_) to avoid collision with native methods or future PHP features.static methods don’t automatically inherit to child classes unless static::class is used inside the closure—test inheritance carefully.method_exists() and is_callable() work, but ReflectionMethod won’t show macros unless you implement custom reflection logic.Collection), use Macroable trait (if supported) or monkey-patch via service provider—but prefer composition over macro injection when possible.__call() magic method; ensure your IDE supports auto-completion for macros via @method annotations or IDE helper packages.How can I help you explore Laravel packages today?