PHP 8.1: New #[ReturnTypeWillChange] attribute

TypeNew Feature

#[\ReturnTypeWillChange] is a new attribute introduced in PHP 8.1, which signals that a mismatching tentative return type should not emit a deprecation notice.

PHP versions prior to 8.0 parses the attribute syntax as a code comment, and does not cause any syntax errors. Adding #[\ReturnTypeWillChange] attribute to a class method does not cause any issues, but omits the deprecation notice in PHP 8.1.

This is the first built-in attribute added to PHP.

class Foo implements ArrayAccess {
    public function offsetGet(mixed $offset) {}
    // ...

When tentative return types are upgraded to standard return types (causing fatal errors if the signature mismatches are not fixed), this attribute will not any effect anymore.

Backwards Compatibility Impact

Unless there is a user-land class declared with name ReturnTypeWillChange in global namespace, this change should not cause any compatibility issues.

Related Changes

RFC Discussion Implementation