A Guide to Union Types in PHP 8: Examples, Best Practices, and Benefits

Moslem Deris
3 min readMar 20, 2023

--

Introduction:

Union types are one of the most exciting new features in PHP 8. They allow developers to define a type that can be either one type or another, which can help to make code more flexible and easier to read. In this article, we’ll explore what union types are, how to use them in PHP 8, and best practices for incorporating them into your code.

What are Union Types?

A union type is a type that can contain values of two or more different types. For example, a union type could contain values of either an integer or a string. In PHP 8, you can declare a union type using the pipe (|) character. Here’s an example:

function getFullName(string $firstName, string|NULL $lastName): string {
if ($lastName === NULL) {
return $firstName;
} else {
return $firstName . ' ' . $lastName;
}
}

In this example, the $lastName parameter is declared as a union type, which means it can either be a string or null. This allows the function to handle cases where a last name may not be provided.

Benefits of Union Types:

Union types offer several benefits to developers, including:

  1. Improved type safety: Union types can help to catch errors at compile-time rather than run-time, which can make code more reliable and easier to maintain.
  2. Increased flexibility: With union types, developers can define a single type that can hold multiple types of data, which can make code more flexible and easier to read.
  3. More expressive code: Union types allow developers to express their intent more clearly by specifying the exact types of data a variable or parameter can hold.

Best Practices for Using Union Types:

When using union types in PHP 8, there are a few best practices to keep in mind:

  1. Keep union types simple: Try to limit the number of types included in a union type to two or three. Adding more types can make code harder to read and understand.
  2. Use union types with caution: While union types can make code more flexible, they can also make it more complex. Use them only when they add real value to your code.
  3. Be consistent: If you’re using union types in your code, try to be consistent in how you declare them. This can help to make your code more readable and easier to maintain.

Examples of Union Types in PHP 8:

Let’s take a look at a few more examples of union types in PHP 8:

function getDiscount(int|float $amount): float {
if ($amount > 100) {
return $amount * 0.1;
} else {
return 0;
}
}

In this example, the $amount parameter is declared as a union type, which means it can either be an integer or a float.

function getAnimal(string $name): string|NULL {
$animals = ['dog', 'cat', 'fish'];
if (in_array($name, $animals)) {
return $name;
} else {
return NULL;
}
}

In this example, the return type of the getAnimal() function is declared as a union type, which means it can either return a string or null.

Conclusion:

Union types are a powerful new feature in PHP 8 that can help to make code more flexible, expressive, and easy to read. By following best practices and using union types judiciously, you can take advantage of this new feature to create more robust and maintainable code. Whether you’re a seasoned PHP developer or just starting out, union types are definitely worth exploring and incorporating into your coding practices. By declaring a union type, you can help ensure that your code is more reliable, easier to maintain, and easier to understand.

In conclusion, union types are a valuable addition to the PHP language, providing developers with a powerful tool for handling data of different types. With union types, you can write more flexible, expressive, and easy-to-read code, and help ensure that your code is more reliable and less error-prone. By following best practices and incorporating union types into your coding practices, you can take advantage of this powerful new feature and make your PHP code more robust and maintainable.

- This article was written with the help of ChatGPT. -

--

--

Moslem Deris
Moslem Deris

No responses yet