Union, intersection, DNF types
A PHP type can name one class, a group of them, or an exact small value. The four shapes below cover the type syntax added from 8.0 to 8.2, and each block runs a function that takes one.
Parenthesized (A&B)|C
Union means or while intersection means and. DNF form groups an intersection in brackets, then joins it with a union.
Read brackets first: the value must match the full bracket group or match the trailing option.
<?php interface HasId { public function id(): int; } interface HasName { public function name(): string; } class User implements HasId, HasName { public function __construct(private int $id, private string $name) { } public function id(): int { return $this->id; } public function name(): string { return $this->name; } } function show((HasId & HasName)|string $v): string { if (is_string($v)) { return $v; } return $v->name(); } echo show("hi"), PHP_EOL; echo show(new User(7, "Ann")), PHP_EOL;
php dnf.php
hi
Ann
The string takes the right side of the union and the object must satisfy both interfaces at once to take the left. An object that implements only one of them is refused before the body runs.
never type
Type never marks a function that does not return at all. It always throws or exits instead of giving back a value.
Use never for guard helpers that stop bad paths so the main code stays flat.
<?php function abort(string $msg): never { throw new RuntimeException($msg); } function root(int $x): float { if ($x < 0) { abort("negative"); } return sqrt($x); } echo root(9), PHP_EOL; try { root(-1); } catch (RuntimeException $e) { echo $e->getMessage(), PHP_EOL; }
php never.php
3
negative
The guard never comes back, so the compiler knows that nothing after it in the negative branch has to return a float; the second call leaves through the exception.
mixed vs explicit
Type mixed accepts anything and skips checks. Explicit unions list only the shapes the code really handles.
Start explicit for public APIs and keep mixed for truly open slots like debug printers.
<?php function dumpAny(mixed $v): string { return get_debug_type($v); } function addNum(int|float $v): float { return $v + 1; } echo dumpAny("hi"), PHP_EOL; echo dumpAny([1, 2]), PHP_EOL; echo addNum(2), PHP_EOL; echo addNum(2.5), PHP_EOL;
php mixed.php
string
array
3
3.5
The mixed parameter takes a string and an array without a word, while the numeric union takes an integer and a float and returns a float for both.
Standalone null false true
Modern PHP lets null, false and true stand alone as types. They describe exact results like a failed search or a strict flag.
A function returning false instead of bool promises that true never appears in that slot.
<?php function findId(array $ids, int $want): int|false { foreach ($ids as $id) { if ($id === $want) { return $id; } } return false; } var_dump(findId([1, 2], 2)); var_dump(findId([1, 2], 5));
php standalone.php
int(2)
bool(false)
The first call finds the value and the second one misses, so the two results are an integer and the literal false the type promises.
Exercises
- Write a function that accepts an object implementing two interfaces or an integer, and print both calls.
- Replace a never guard with an exception thrown inline and describe the difference for the reader.
- Add an int|string parameter to addNum() and decide what the function should do with a string.
- Give a search function the return type ?string and then string|false and compare the meanings.
- Print get_debug_type() for a null, a float and an object.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the intersection in
(HasId & HasName)require?Answer
Both interfaces on the same object
The ampersand is an and: the value must satisfy every member of the group.
-
What does the union bar in
(HasId & HasName)|stringmean?Answer
The value may match the group or be a string
The bar is or, so either the bracket group or the string is accepted.
-
What may a function declared
: neverdo?Answer
Throw or exit without returning
never says the call does not come back with a value at all.
-
What does the type
mixedaccept?Answer
Any value
mixed is the widest type and checks nothing, which is why explicit unions are preferred in APIs.
-
What does this program print?
<?php interface HasId { public function id(): int; } interface HasName { public function name(): string; } class User implements HasId, HasName { public function __construct(private string $name) { } public function id(): int { return 7; } public function name(): string { return $this->name; } } function show((HasId & HasName)|string $v): string { if (is_string($v)) { return $v; } return $v->name(); } echo show(new User("Ann"));Answer
Ann
The object satisfies both interfaces, so the function takes the left side and returns its name.
-
What does this program print?
<?php function abort(string $msg): never { throw new RuntimeException($msg); } try { abort("stop"); } catch (RuntimeException $e) { echo $e->getMessage(); }Answer
stop
The guard throws, the catch takes the exception and prints its message.
-
What does this program print?
<?php function dumpAny(mixed $v): string { return get_debug_type($v); } echo dumpAny([1, 2]);Answer
array
get_debug_type() names the type of the value it received, and an array prints as array.
-
What does this program print?
<?php function addNum(int|float $v): float { return $v + 1; } echo addNum(2.5);Answer
3.5
The float side of the union takes 2.5, and the result of the addition is 3.5.
-
What does this program print?
<?php function findId(array $ids, int $want): int|false { foreach ($ids as $id) { if ($id === $want) { return $id; } } return false; } var_dump(findId([1, 2], 5));Answer
bool(false)
No element matches, so the function returns the literal false the type allows.
-
Why is
falsemore precise thanboolas a return type?Answer
It promises that true never comes back from that call
A literal type rules out the other value, so a caller reads the exact result set.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev