Type %s cannot be part of an intersection type

Description

An intersection type, like string&Iterator, requires a value to be an instance of every listed type at once. That is only meaningful for class and interface names: a value can implement several interfaces simultaneously.

Scalar and compound pseudo-types (int, float, string, bool, array, callable, iterable, mixed, void, never, null, false, true), as well as self, parent and static, cannot be combined into an intersection, since nothing can be, for instance, a string and an object implementing Iterator at the same time.

Example

<?php

function foo(): string&Iterator {}

?>

Literal Examples

  • Type string cannot be part of an intersection type

Solutions

  • Remove the scalar or pseudo-type from the intersection, and keep only class or interface names.

  • Use a union type (|) instead, if any of the alternatives is acceptable.

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>`_.