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 don’t work with scalar values, which are excluding one another. It works with polymorphism, where one object may be of several types at the same time, when implementing interfaces.
<?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
Added in PHP 8.1