Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Composite Type

Composite types combine multiple types into a single type expression. PHP supports three forms of composite types:

  • Union types A|B: the value must satisfy at least one of the listed types. Introduced in version 8.0, with catch clauses supporting multi-type syntax since PHP 7.1.
  • Intersection types A&B: the value must satisfy all of the listed types simultaneously. Restricted to class and interface names. Introduced in version 8.1.
  • DNF types (A&B)|C: disjunctive normal form, which combines intersections and unions. Introduced in version 8.2.

Composite types are accepted in parameter types, property types, and return types. catch clauses accept union types only. instanceof does not accept any composite form.

<?php

    // Union type
    function trumpet(int|string $id): void {}

    // Intersection type
    function forage(Countable&Iterator $col): void {}

    // DNF type
    function migrate((Countable&Iterator)|null $col): void {}

?>

Documentation

See Also