Skip to content

Replace #[\ReturnTypeWillChange] with native return type declarations #20745

@WarLikeLaux

Description

@WarLikeLaux

The framework currently uses #[\ReturnTypeWillChange] in 17 files (72 methods) to suppress PHP 8.1+ deprecation warnings. This attribute will stop working in PHP 9.0, causing fatal errors for any method with an incompatible return type.

In PHP 9.0, incompatible return types in methods overriding internal methods will result in a fatal error, regardless of whether #[\ReturnTypeWillChange] is present.

Affected files

File Methods Risk
web/SessionIterator.php 5 Low (internal class)
web/SessionHandler.php 5 Low (internal class)
web/HeaderCollection.php 5 Low
web/CookieCollection.php 5 Low
web/Session.php 5 Medium
db/BatchQueryResult.php 5 Low (internal class)
db/DataReader.php 5 Low
db/JsonExpression.php 1 Low
db/ArrayExpression.php 5 Low
db/SqlToken.php 5 Low
db/mssql/DBLibPDO.php 2 Low (internal class)
db/mssql/PDO.php 2 Low (internal class)
db/mssql/SqlsrvPDO.php 2 Low (internal class)
db/BaseActiveRecord.php 5 High (widely extended)
base/Model.php 5 High (widely extended)
base/ArrayAccessTrait.php 5 High (used by Model)
caching/Cache.php 5 Medium

BC impact

Adding return types to non-final classes is technically a minor BC break: any child class overriding these methods without matching return types will get a fatal error. However, most of these methods implement PHP built-in interfaces (Iterator, ArrayAccess, Countable) and child classes will need the same return types anyway for PHP 9.0 compatibility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    22Yii 2.2

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions