Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP return type hinting, object OR a boolean? [duplicate]

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?

like image 667
life Avatar asked Apr 26 '16 19:04

life


People also ask

How to specify return type in php?

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...

What is type hinting in PHP?

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.

What is the difference between Bool and boolean in PHP?

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.

What is return true in PHP?

Sometimes a method/function returns a boolean value to indicate if the operation was succesfull. In the given example it always returns "TRUE".


1 Answers

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:

  1. Successfully found results
  2. Successfully found no results
  3. 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
 */
like image 55
Machavity Avatar answered Oct 25 '22 14:10

Machavity