1.2.943. PHP Native Interfaces and Return Type

Native PHP interface which define a type, expect the derived methods to use the same time. In particular, a mixed return type was added to the jsonSerialize() of the JsonSerialize PHP interface.

In PHP 8.1, the mixed return type is now enforced, and a deprecated notice is displayed.

One solution is to add the good return type, or to use the #[`ReturnTypeWillChange]` attribute. This rule covers the following interfaces :

<?php
    class MyJsonSerialize implements jsonserialize {
        function jsonserialize() : int {}
    }
?>

See also JsonSerializable::jsonSerialize.

1.2.943.1. Suggestions

  • Add the mixed returntype to all implementation of the jsonSerialize method

  • Add the #[ReturnTypeWillChange] attribute to the method

1.2.943.2. Specs

Short name

Php/JsonSerializeReturnType

Rulesets

All, Analyze, Changed Behavior, CompatibilityPHP81, Deprecated, LintButWontExec

Exakat since

2.3.0

PHP Version

With PHP 8.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

json

Note

This issue may lint but will not run

Available in

Entreprise Edition, Exakat Cloud