PHP 8.0.0 adds the \Stringable interface, which assures the existence of the __toString magic method on any class that implements it. If MarkupInterface were to extend it, this would mark all Markup objects as being safely castable to string, allowing Drupal-independent code to accept/return it anywhere string|\Stringable is type-hinted.
Arguments for making this change in a D9 minor version:
- Even though Drupal 9 does not yet require PHP 8, we already have access to \Stringable via the symfony/polyfill-php80 package
- \Stringable is exactly equivalent to MarkupInterface in that both only contain the __toString() method, so extending it doesn't alter any contracts or break BC.
Comments
Comment #2
cburschkaComment #5
smustgrave commentedThis seems valid and correct. Will let committers decide.
Comment #6
catchSo this is good, but there ought to be some places we can change type hints from string|MarkupInterface to string|Stringable in core too? I think we should open a plan issue to discuss. That doesn't detract from adding the interface to MarkupInterface itself so it can be passed to methods that already type hint string|Stringable.
Comment #7
quietone commentedAdding tag for #6
Comment #9
catchOpened #3340879: Change type hints from string|MarkupInterface to string|Stringable .
Committed 17aefff and pushed to 10.1.x. Thanks!
Comment #10
wim leers🙏 Yay! This has gotten in the way for me before — this would've made that painless instead of painful 😊