- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Union Type in PHP 8
Using Union Type in PHP 8, we can use the values of two or more types, instead of using a single type. To specify multiple types, vertical line (|) is used to join them.
Union type supports parameters, return types, and properties.
Syntax
type1|type2|……|type_n
Example 1: Union Type
<?php class Number { private int | float $num; public function getNum(): float | int { return $this->num } public function setNum(float | int): void { $this->num = $num; } } ?>
Example 2: PHP 8 Program using Union Type
<?php class Number{ private int|float $number; public function setNumber(int|float $number): void { $this->number = $number; } public function getNumber(): int|float { return $this->number; } #passing floats or integer values to the number object } $number = new Number(); $number->setNumber(5); print_r($number->getNumber()); $number->setNumber(11.54); print_r($number->getNumber()); ?>
Output
511.54
Nullable Types in Union Type
In PHP 7.1, nullable type is used with the question mark ?type. In PHP 8, we can declare nullable types as type|null. For example: float|int|null, but we cannot declare it as ?float|int.
Nullable Types Syntax
type1|type2|null
We should not declare like ?type1|type2 because this would be an ambiguous declaration.
Compile-time error checking − In PHP, union type duplicate or redundant types in a type declaration are not allowed. This check will happen during compile-time without autoloading classes/interfaces.
Duplicate types are not allowed − We cannot declare duplicates in PHP like int|int and also int|?int is not allowed. Because it will return a syntax error. "Fatal error: Duplicate type… is redundant in …on line."
void type is not allowed − In PHP 8, union type void is not allowed to combine with any other type.
Example
Function foo(): void|null{}
False Type in PHP 8
False type is a part of Union type in PHP 8. Mostly we use null as return values to show the absence of a return value or an error. For example: Suppose we have a class method that finds the student by the student ID. If Student ID is not present, it might give null to show that Student ID was not present. But PHP uses false if the Student ID is not found.
False type is used for any failure instead of null type.
We cannot use false as a standalone type and also nullable standalone types are not allowed. That means, we cannot use false, false|null, and ?false.
We can use false wherever types are accepted like a parameter, properties, and return values.
It has no true literal type, instead we can only use bool types.
Example
<?php Class Student { public function find(int $id): User|false { // ... } } ?>