Allow Macroables to override defined methods #42915
-
I'd like to be able to override defined methods on Macroables. An example would be overriding the Str::start('+200', ['+', '-']); // outputs '+200'
Str::start('-200', ['+', '-']); // outputs '-200'
Str::start('200', ['+', '-'], '+'); // outputs '+200' (using the value of the third argument as the "default") Currently, if I declare a macro like so: Str::macro('start', function($string, $possibilities, $default = null){
return 'my custom +/- functionality';
}); The macro closure doesn't get executed when I call One option would be to declare all I'm happy to make a PR but I guess I need a little inspiration. Any ideas other ways to achieve this functionality without resorting to a rewrite of all public methods of any Macroable classes? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 20 replies
-
The Facades is also using __callStatic. So a check could be done there, or a proxy method could be added. Instead of The problem may lay in the expectations. if I use a function called lowercase to do lowercase, I do not want it to transform it from pascalcase to snakecase. Your example could be a valid usecase but you cannot disallow others without doing extensive inspections, which may or may not impact overall performance. basically, I like, but I also think it's a bad idea. Macroable was created to add functionality to classes from third parties, not to modify it's behavior. |
Beta Was this translation helpful? Give feedback.
-
There isn't a facade for
That's a pretty strange assumption there. If it wasn't created to "modify it's behavior" then why isn't |
Beta Was this translation helpful? Give feedback.
-
My current solution works quite well, although it doesn't use macros. It's a more traditional object oriented approach to extension. I modified the Maybe this solution is as good or better than using macros to override methods (if that were possible). It would still be nice to be able to override macros; it would be more plug-and-play. |
Beta Was this translation helpful? Give feedback.
-
@taylorotwell Can moderators please act so that this repo doesn't become an unpleasant place to contribute? |
Beta Was this translation helpful? Give feedback.
-
Hey all - this thread has gotten a bit out of hand. We have to keep Laravel a chill, cool place for everyone to talk and contribute. Have instituted a temporary block on a couple users to let things cool down. |
Beta Was this translation helpful? Give feedback.
-
To answer the original question, I think that the two options (kinda) already discussed are equally valid, with the final choice depending on whether you believe that this functionality would ever be included in Laravel or not:
|
Beta Was this translation helpful? Give feedback.
My current solution works quite well, although it doesn't use macros. It's a more traditional object oriented approach to extension.
I modified the
Str
alias inconfig/app.php
to point to my ownStr
class that extends the IlluminateStr
class. This is the best way I currently know how to "override"Str
methods. This covers most usage, but not if developersuse Illuminate\Support\Str;
directly. From working on various projects, it’s quite common for developers to do that, so if I want to override a method or two I need to go through the whole project and replace all occurrences ofuse Illuminate\Support\Str;
. It also doesn’t cover using theStr
class alias before Laravel boots, eg. in a co…