Context:
In PHP 8.1, specifying return types for methods becomes more prevalent, leading to conflicts with existing implementations.
Problem:
When a method's return type changes from a compatible type to an incompatible type or is unspecified, the following deprecation notice appears:
Deprecated: Return type of [Method Name] should either be compatible with ..., or the #[\ReturnTypeWillChange] attribute should be used...
Implications of Return Type Covariance:
PHP 7.0 introduced return types, ensuring consistent behavior for calling code based on the specified contract. Extension or implementation classes must maintain the same or more specific return types.
Background of Deprecation:
With the addition of Union Types in PHP 8.0, internal functions and methods gained return type specifications. However, enforcing these types would break backward compatibility. Instead, a deprecation notice was introduced for tentative changes.
Purpose of #[\ReturnTypeWillChange] Attribute:
This attribute indicates a planned change in return type. PHP 8.1 ignores the deprecation notice for methods marked with this attribute, allowing support for older PHP versions.
What To Do:
Note: Enforced return types are likely in PHP 9.0, so plan to address #[\ReturnTypeWillChange] attributes accordingly.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3