Reference: Return Type of ... Should Be Compatible with ... or the #[ReturnTypeWillChange] Attribute Should Be Used
Background: Return Types and Covariance
Covariance rules in PHP dictate that return types in extended classes must be compatible with or more specific than those in the parent class. However, extending a class with a different or missing return type can break the covariance assumption.
Backwards Compatibility and Deprecation
Introducing return types to existing methods would break code relying on older PHP versions without them. Hence, PHP uses tentative return types with deprecation notices to allow adaptations.
The #[ReturnTypeWillChange] Attribute
The #[ReturnTypeWillChange] attribute informs PHP 8.1 to suppress the deprecation notice for a method with a planned return type change. This grants time for updates without immediate breakage.
Resolution
Determine the affected method and correct return type from the deprecation notice. Options for resolution include:
The above is the detailed content of What To Do About Deprecation Notices When Return Types Change in PHP?. For more information, please follow other related articles on the PHP Chinese website!