-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[5.8] Less Greedy Mutator #29392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.8] Less Greedy Mutator #29392
Conversation
this allows us to return a non-`null` value from our Model mutator in order to continue through the `setAttribute()` method. this maintains backwards compatibility because currently no values are returned from mutators, from which PHP will inherently return `null`. https://www.php.net/manual/en/functions.returning-values.php
Your change would not account for date casts as it's in the Although I understand it's an optional feature:
I think a setter (and getter) should be as transparent as possible and this goes into the opposite direction. |
the only way you would make it to this conditional is if the previous `if` was false, so being an `if` or an `elseif` doesn't really make a difference.
Fair point about the date casting. I've switched the I don't think this is adding any more magic than currently exists. With a couple lines of docs it will be easily understandable. Can you explain this line a little more?
We are still setting the attribute, we are just allowing the |
I think it's not ideal that you can't set an attribute to If we merge this, we should target 6.0. I'm sure there's someone who returns |
@staudenmeir good point about setting a For someone returning |
Thanks for your work @browner12 , I've changed my mind regarding "all that magic". However I'm too a bit worried about the |
With the null case and potential breaking changes here I would prefer to just stick with the solution offered in the previous attempt of explicitly calling |
this is a retry of #29375. it's much cleaner, and allows users to tie into existing mutator methods if they wish to utilize it.
currently mutators manually set values in the attributes property and return early in the
setAttribute()
method. This behavior will continue unaffected with this change.However, now you can
return
a value from the mutator method instead, which will cause it to also run through any additional logic in thesetAttribute()
method.The most common use case is when we want to alter some data before it is passed to the normal casting logic.
this maintains backwards compatibility because currently no values are returned from mutators, from which PHP will inherently return
null
.https://www.php.net/manual/en/functions.returning-values.php