Conversation
Levdbas
left a comment
There was a problem hiding this comment.
This seems like a logical thing to do. I do think we need to document this change in the upgrade guide as well for folks that extended these classes in the past and maybe have overwritten the constructor?
|
@Levdbas I added the notes in the Upgrade Guide: After that, we can merge this. |
Levdbas
left a comment
There was a problem hiding this comment.
Yes, this seems good. Maybe we can add a howto guide on what the recommended way is to extend a Timber object and how to call it from your code. But that is out of the scope of this PR.
|
@Levdbas Thanks! I think we already covered how to extend Timber objects in https://timber.github.io/docs/v2/guides/extending-timber/, right? |
Related:
Issue
PHPStan reports "Unsafe usage of new static()" for some objects on level 0. What this means and how to solve this can be read in https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static.
Solution
Make the object constructors final. Not being able to overwrite a constructor seems like a good idea to me. As noted in the linked post:
By making the constructors final, I already ran into an issue in
PagesMenu, where I used a custom constructor. Removing the constructor and just updating the values on the object (or through a custom method) works fine.This change was already discussed in #2203 (comment). In that discussion, there’s an example that wouldn’t work anymore with the change.
Am I wrong to assume that this could be rewritten with a custom method
do_something()that doesn’t change the constructor?Impact
With final constructors, it will be easier to update constructors in the future, because we will not break backwards compatibility automatically. Otherwise, we’d have to assume that any constructor could have been overwritten.
Usage Changes
We won’t be able to overwrite the constructors in classes that extend the Timber core object. But it will still be possible to update the objects with other custom methods as shown above.
Considerations
In case we need some other functionality to make it easier to solve a certain use case, I’m sure we can come up with a proper API for that that doesn’t involve custom constructors.
Testing
Nothing in particular.