1.2.1260. Union Typehint

Union typehints allows the specification of several typehint for the same argument or return value.

Several typehints are specified at the same place as a single one. The different values are separated by a pipe character |, like for exceptions Nullable is reported as union type. Mixed and iterable are not reported as a union type.

Union types are a PHP 8.0 new feature. They are not compatible with PHP 7.4 and older.

<?php

// Example from the RFC https://wiki.php.net/rfc/union_types_v2
class Number {
    private int|float $number;

    public function setNumber(int|float $number): void {
        $this->number = $number;
    }

    public function getNumber(): int|float {
        return $this->number;
    }
}
?>

See also PHP RFC: Union Types 2.0, PHP 8 Union types and Type declarations.

1.2.1260.1. Specs

Short name

Php/Php80UnionTypehint

Rulesets

All, Appinfo, CE, CompatibilityPHP70, CompatibilityPHP71, CompatibilityPHP72, CompatibilityPHP73, CompatibilityPHP74

Exakat since

2.0.9

PHP Version

With PHP 8.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

typehint, intersection-type

Available in

Entreprise Edition, Community Edition, Exakat Cloud