Trusted answers to developer questions

What are "union types" and how do you use them in PHP?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

In this shot, I want you to learn about a PHP feature called “union type” and how to implement or use it both in PHP 7 and PHP 8. Without any further ado, let’s get started.

What is a union type?

Generally speaking, union types are defined as follows:

A collection of values stored within the same position in memory with different data types/formats.

In short, union type allows you to define a collection of two or more types (int, float…) for your variables or function parameters. For example, you can have a function in your program that accepts a parameter of the int or string type.

The union type’s typical syntax looks like this:

public function foo(Foo|Bar $input): int|float;

The foo function (above) accepts a parameter, $input, of type both Foo and Bar, and returns a value of either an int or float.

Union type in PHP 7

PHP 7 only supports these two special union types:

  • Nullable union types using the ?Type notation: public function bar(?Bar $bar): void;
  • Array or Traversable using the iterable type

PHP 7 does not support arbitrary union types, but as a workaround, we can use phpdoc annotation. Let’s see an example of its use below:

<?php
class Student
{
/**
* @var int|float $roomNo
*/
private $roomNo;
/**
* @param int|float $roomNo
*/
public function setRoomNo($roomNo) {
$this->roomNo = $roomNo;
}
/**
* @return int|float
*/
public function getRoomNo() {
return $this->roomNo;
}
}
$newStudent1 = new Student;
$newStudent1->setRoomNo(25.0);
$newStudent2 = new Student;
$newStudent2->setRoomNo(4);
echo "Student 1 number is " .$newStudent1->getRoomNo(). "<br>";
echo "Student 2 number is " .$newStudent2->getRoomNo();

Now, let’s look at how “union type” is improved in PHP 8.

Union type in PHP 8

As we’ve seen above, PHP 7 lacks built-in support for arbitrary union types. So, PHP 8 has implemented built-in support for arbitrary union types.

With the same example as above, here’s how you can make use of union types in PHP 8:

<?php
class Student
{
private int|float $roomNo;
public function setRoomNo(int|float $roomNo): void {
$this->roomNo;
}
public function getRoomNo(): int|float {
return $this->roomNo;
}
}
$newStudent1 = new Student;
$newStudent1->setRoomNo(25.0);
$newStudent2 = new Student;
$newStudent2->setRoomNo(4);
echo "Student 1 number is " .$newStudent1->getRoomNo(). "<br>";
echo "Student 2 number is " .$newStudent2->getRoomNo();

Note

  • the void type can never be part of a union type because it indicates that the function does return any value
  • nullable union type can also be written as: public function foo(Foo|null $foo): void;

Conclusion

Union types are a great functionality in PHP that let us define a variable of multiple types in our program. PHP 7 supports arbitrary union types through phpdoc annotation, whereas PHP 8 has built-in support for it.

RELATED TAGS

php
Did you find this helpful?