The ReturnTypeWillChange attribute

(PHP 8 >= 8.1.0)

Introduction

As of PHP 8.1.0, a tentative phase of transitioning to return type declarations for internal class methods began.

Most non-final internal methods now require overriding methods to declare a compatible return type. Otherwise, a deprecation notice is emitted during inheritance validation warning that the signature violates covariance rules.

In a future PHP version, method signature checking will become strict, and mismatches will cause a fatal error.

In case the return type cannot be declared for an overriding method due to PHP cross-version compatibility concerns, or the overriding method declares an incompatible return type, a #[\ReturnTypeWillChange] attribute can be added to silence the deprecation notice.

Warning

When overriding methods defined in user-defined classes, return types are checked strictly. A fatal error is emitted on mismatch, even if the overriding method is marked with the attribute.

The ReturnTypeWillChange attribute suppresses deprecation warnings only during the tentative return type phase. After strict type checking is enforced, the attribute will stop working and a mismatch in the overriding method signature of an internal class will cause a fatal error.

Class synopsis

#[\Attribute]
final class ReturnTypeWillChange {
/* Methods */
public function __construct()
}

Table of Contents

add a note

User Contributed Notes

There are no user contributed notes for this page.