Source: www.amitmerchant.com

Intersection types in PHP 8.1

Category: PHP, javascript

When working with types in any language there is a couple of scenarios that can occur. Allow properties to be typed using multiple different types where the property must match at least one of the types.

PHP has been already supporting the first scenario in form of https://www.amitmerchant.com/union-types-php/ since PHP 8.0 where you can type the properties using multiple types like so. class Number { private int|float $number; public function setNumber(int|float $number): void { $this->number = $number; } public function getNumber(): int|float { return $this->number; }}

Currently, only class types (interfaces and class names) are supported by intersection types due to some of the edge cases.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners