Glossary

Union and Intersection Types

Union types (PHP 8.0) let you declare that a parameter or return value can be one of several types. Intersection types (PHP 8.1) require the value to implement all listed interfaces simultaneously.

Union Types

function parse(string|int $id): User|null {
  return User::find($id);
}

// PHP 8.0 also added: int|string, float|int, etc.
// Special types: mixed, never, void

Intersection Types

interface Serializable {}
interface Loggable {}

function process(Serializable&Loggable $obj): void {
  // $obj is guaranteed to implement both interfaces
}

DNF Types (PHP 8.2)

Disjunctive Normal Form — combining union and intersection: (Serializable&Loggable)|null.