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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners