Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Union types are coming in PHP 8

In this article, I’m going to discuss about the union types which are going to come in future versions of PHP through this RFC. To understand why and how are union types important in PHP, let’s first understand what are union types.

What are union types?

According to Wikipedia,

In computer science, a union is a value that may have any of several representations or formats within the same position in memory; that consists of a variable that may hold such a data structure.

So basically, A union type describes a value that can be one of several types. A “union type” accepts values of multiple different types, rather than a single one. This means if the programming language supports union types, you can declare a variable in multiple types. For instance, there can be a function which can accept variable of type “string” or “float” as a parameter.

When it comes to PHP, it already supports two special union types:

  • Type or null, using the special ?Type syntax.
  • array or Traversable, using the special iterable type.

But as of now, arbitrary union types are not supported by the language. So, programmers tends to use PHPDoc annotations as a workaround.

class Number {
    /**
     * @var int|float $number
     */
    private $number;

    /**
     * @param int|float $number
     */
    public function setNumber($number) {
        $this->number = $number;
    }

    /**
     * @return int|float
     */
    public function getNumber() {
        return $this->number;
    }
}

But this is going to be changed because union types are coming to PHP natively. An rfc proposal has been approved which adds the support for the union types in PHP. Accoring to this proposal, Union types are specified using the syntax T1|T2|... and can be used in all positions where types are currently accepted. Like the following example:

class Number {
    private int|float $number;

    public function setNumber(int|float $number): void {
        $this->number = $number;
    }

    public function getNumber(): int|float {
        return $this->number;
    }
}

You may also like: Nullable types in PHP 7.1

Advantages of using union types

As you can see in the example above, the syntax is now a lot less boilerplate-y than phpdoc. Apart from this, having the union types can bring following advantages:

  • Types are actually enforced, so mistakes can be caught early.
  • Because they are enforced, type information is less likely to become outdated or miss edge-cases.
  • Types are checked during inheritance, enforcing the Liskov Substitution Principle.
  • Types are available through Reflection.

Scope of union types

Union types will support all types currently supported by PHP. There are some exceptions to this such as void type, Nullable union types, false pseudo-type and so on. You can read more about it here.

In closing

The union types are all great and they are most probably going to be included in PHP 8.0. So, until then we’ll have to resort to the good old PHP DOCBlocks.

More in PHP 8

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?