PHP 8.1 added intersection types and the never return type. PHP 8.2 added readonly classes and fixed many type inconsistencies. Each version makes PHP safer and more expressive for building modern applications.
Intersection Types (PHP 8.1)
interface Countable { public function count(): int; }
interface Serializable { public function serialize(): string; }
// Intersection — must implement BOTH
function processCollection(Countable&Serializable $col): void {
echo $col->count();
echo $col->serialize();
}
// vs Union types (either one)
function process(Countable|Serializable $col): void {}
// Never type — function always throws or exits
function fail(): never {
throw new RuntimeException("Always fails");
}
// DNF types PHP 8.2 — combine intersection and union
function handle((Countable&Serializable)|null $col): void {}