What you're talking about is called a Union Type. There's considerable discussion about it in Internals
This RFC proposes the ability to define multiple possible types for a parameter or return type and calls them “union types”. A value passes the type-check for a union type if the value would pass any one of the members the union. A vertical bar (OR) is placed between each of the two or more types.
Here is an example of a parameter accepting either an array or a Traversable and no other types:
function (Array | Traversable $in) {
foreach ($in as $value) {
echo $value, PHP_EOL;
}
}
There can be more than two types in the union. As an example, it is somewhat common for a routine that interacts with a database to have one of three results:
- Successfully found results
- Successfully found no results
- There was an error
This is all targeted at PHP 7.1 but isn't up for a vote yet (let alone looking like it will pass).
So what about your issue? I would say, at least for now, don't type hint your return. Just issue a doc block that says it can return User
or false
/**
* @param \PDO $pdo
* @param string $username
* @return User|false
*/