Union Type

Union types refer to the ability to specify multiple possible types for a property, parameter, or return value. It allows a property or parameter to accept values of different types. It allows a method or function to return values of different types. Union types were introduced in PHP 8.

Before PHP 8, type declaration could only have a single type declaration, except for the null type. Nowadays, with union types, the code can declare multiple types by separating them with a pipe | symbol.

Union types were introduced for exception catching, before PHP 8.

<?php

    class X {
        private A | B | C $property;
    }

    try {

    } catch (A|B|C $e) {

    }

?>

Documentation

See also What are union types and how do you use them in PHP?.

Related : Type System, Alias Types, Disjunctive Normal Form (DNF), Literal Types, Relative Types, Property Type Declaration, Intersection Type, Named Type, Type Inference

Added in PHP 8.0