Type %s contains both object and a class type

Description

The object type already matches an instance of any class. Adding a specific class name next to it in a union type, such as object|Test, does not widen what is accepted: every value that matches Test already matches object. PHP detects this redundancy purely from the type names, without needing Test to be defined or loaded.

The full message reads: Type %s contains both object and a class type, which is redundant.

Example

<?php

function test(): object|Test {
}

?>

Literal Examples

  • Type Test|object contains both object and a class type, which is redundant

Solutions

  • Remove the specific class name, and keep only object.

  • Remove object, and keep only the specific class name, if that narrower type is what is actually intended.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.