An alternative approach for PHP 8.1 support #340
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@Minishlink I am so sorry seeing as you already (very efficiently!) merged my previous PR.
There is an alternative approach. I'm not exaggerating when I say I've spent the last 24 hours engrossed in various vendor libraries looking at this kind of stuff with a view to supporting all PHP versions from 7.0 to 8.1 in our application and it's starting to affect my ability to make correct decisions!
PHP 8.1 adds a return type of
mixed
to theJsonSerializable::jsonSerialize
method. While the#[\ReturnTypeWillChange]
attribute will work, an alternative is to simply add thearray
return type.This will work on all PHP versions you support and still work on PHP 8.1 because
array
is a more specific type thanmixed
(which is a union type that consists of many types, includingarray
).So, nothing wrong with the previous approach but this may be preferred. No issue if you decide to close this in favour of sticking with the original approach.