mpstenson
mpstenson

Matt Stenson

@mpstenson

I work as UserScape in php / laravel development and devops. Recreationally, a skier, mountain biker and general mountain person.

5 Posts 48 Views

  • No matching results...
  • Searching...

/ 255

I was recently working on a project where I wanted to make a classes functions available inside of the built in Laravel Str:: helper. Laravel Macros are the answer to this issue. However, I wanted to make adding additional methods an easy experience and needing to write and maintain a separate macro with argument definitions for each function was clunky.

foreach (get_class_methods(AdvStr::class) as $methodName) {
Str::macro($methodName, function () use ($methodName) {
$args = func_get_args();

return (new AdvStr())->$methodName(...$args);
});


explanation -> mattstenson.com/dynamically-macro-a-class-to-a-laravel-macroable-class

130