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 version 8.0.
Before PHP 8, a type declaration could only have a single type, 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 Elephpant {
private Herd | Waterhole | Matriarch $tusk;
}
try {
} catch (Herd|Waterhole|Matriarch $e) {
}
?>