The ReturnTypeWillChange attribute

(PHP 8 >= 8.1.0)

Introduction

Most non-final internal methods now require overriding methods to declare a compatible return type, otherwise a deprecated notice is emitted during inheritance validation. This introduces a tentative return type phase: the engine emits a deprecation notice instead of a fatal error when return types are incompatible, before they become enforced in a future version. In case the return type cannot be declared for an overriding method due to PHP cross-version compatibility concerns, a #[\ReturnTypeWillChange] attribute can be added to silence the deprecation notice.

Warning

The ReturnTypeWillChange attribute suppresses deprecation warnings during the tentative return type phase only. It has no effect when overriding methods defined in user-defined classes. Once internal methods adopt strict types, mismatches in overriding method signatures will cause a fatal error and this attribute will no longer have any effect.

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.