Intersection Type¶
Intersection types, also called intersectional types, is a specification syntax where several types act together as a single type. The individual types are separated with the & operator.
The value typed with an intersection type must satisfy all the types at the same time.
Intersection types only accept class and interface types: scalars (int, string, bool, etc.), null, false, true, and void are not allowed, as a single value cannot simultaneously be of two scalar types. Redundant types are also rejected (e.g., A&A).
Intersection types work well with polymorphism: an object that implements multiple interfaces satisfies an intersection type naturally.
PHP 8.2 introduced Disjunctive Normal Form (DNF) types, which combine intersection and union types, such as (A&B)|null.
<?php
class x {
// Property is of type A, B and C at the same time.
// Two of them have to be interfaces.
private A & B & C $property;
}
?>
See also How the New Intersection Types in PHP 8.1 Give You More Flexibility.
Related : Type System, Disjunctive Normal Form (DNF), Named Type, Relative Types, Property Type Declaration, Union Type, Interface, Polymorphism, Nullable, Return Type, TypeError
Added in PHP 8.1