Replies: 1 comment 1 reply
-
It would be a laravel 10 solution for sure. 'accessing the macros array directly' shouldn't be worried about, accessing internals is a risk the developer takes on his own. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The current mechanism of the Macroable trait overrides the parent's macros when using the same name. Check this test for example:
The first assertion will fail. That's actually a side-effect of a bigger issue - The Macroable class and all of its descendants share the same macros array, which means any macro of a sub-class will override the parent's one (if they're using the same name), and will also be available for any descendant of the parent class (e.g. let's say we have classes B & C that both extend A. When adding a macro to C it'll be available to A, B & C).
I think the expected behavior is to be somewhat similar to pure OOP:
I'm not sure how we can solve this (unless there is some black magic that I'm not aware of 😅).
The only way that came to my mind is to change the macros array structure a little bit so it won't be flat, maybe something like this:
And then when calling the macro, we can use ReflectionClass::getParentClass() recursively to pull from the array.
I'm not sure that I like this approach, it feels kinda fragile & complicated because we need to implement inheritance ourselves, and using reflection might have some performance penalty (even though I've heard it's not that bad, but I don't really know).
Plus, it probably won't be backward compatible if someone (for some reason) accessed the macros array directly from outside of the trait.
Any ideas if and how we can solve this properly?
Beta Was this translation helpful? Give feedback.
All reactions