Return type of %s::%s() should either be compatible with %s::%s(): mixed¶
Description¶
Native PHP interfaces, which define a type, expect the derived methods to use the same time. In particular, a mixed`
return type was added to some interfaces and must be available with child classes.
Since PHP 8.1, the mixed return type is now enforced, and a deprecated notice is displayed.
One alternative is to add the expected return type; a temporary solution is to the #[ReturnTypeWillChange] attribute. The attribute makes the warning disappear, but must be updated in the long run to keep in line with PHP recommendations.
This rule covers the following interfaces :
ArrayAccess
Countable
Exception
FilterIterator
Iterator
JsonSerializable
php_user_filter
SessionHandlerInterface
Example¶
<?php
class MyJsonSerialize implements JsonSerialize {
function jsonserialize() : int {}
}
?>
Literal Examples¶
Return type of MyJsonSerialize::jsonserialize() should either be compatible with JsonSerialize::jsonserialize(): mixed, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice
Solutions¶
Use the correct return type for the methods from the interfaces.
Use the the
#[ReturnTypeWillChange]
to hide the error message.Remove the interface from this class.
Remove the offending method.