So I know that I can do return type hinting in php7. I can do an object return hint with:
function getUser($pdo, $username) : User
{
}
where User is the object being returned.
However, if the user isn't found in the SQL, returning 'false'
instead of a User object gives:
Uncaught TypeError: Return value of UserFind::findUser() must be an instance of User, boolean returned
But what if the SQL can't find the user? How can I return a boolean, false, if the user doesn't exist? Should I just ignore return type hinting in this scenario?
EDIT: I looked at the other question, 'Nullable return types in php 7' and while my question is almost identical, I want to extend my question by asking if there would ever be a way to return one of two types. For example return an object or a string if the object is nonexistant?
php declare(strict_types = 1); function returnIntValue(int $value): int { return $value + 1.0; } print(returnIntValue(5)); ?> Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned...
Type hinting is a concept that provides hints to function for the expected data type of arguments. For example, If we want to add an integer while writing the add function, we had mentioned the data type (integer in this case) of the parameter.
So in a nutshell, boolean is an alias for bool , and aliases don't work in type hints. There are no similarity between Type Hinting and Type Casting . Type hinting is something like that you are telling your function which type should be accepted. Type casting is to "switching" between types.
Sometimes a method/function returns a boolean value to indicate if the operation was succesfull. In the given example it always returns "TRUE".
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
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With