Niko9911
Niko9911

Reputation: 371

PHP 7: Multiple function return types

So, now we got new PHP7 and we can check if return type is what we want.

For example,

function foo(): bool
{
    $num = 8;
    if (10 === $num) {
        return true;
    } else {
        return false;
    }
}
foo();

OUTPUT: false

Okey, that was easy? It works like it should, but if return is not what we expect?

function bar(): bool
{
    $num = 10;
    if (10 === $num) {
        return array(['apple', 'banana', 'strawberry']);
    } else {
        return false;
    }
}
bar();

OUTPUT: Uncaught TypeError: Return value of foo() must be of the type boolean, array returned

That was very basic and those examples just show how it works.

If we have function like in code example 2, can we check for multiple return types? Like

function bar(): bool || array    <---
{
    $num = 10;
    if (10 === $num) {
        return array(['apple', 'banana', 'strawberry']);
    } else {
        return false;
    }
}
bar();

But however this results: FATAL ERROR syntax error, unexpected '||' (T_BOOLEAN_OR), expecting '{' on line number 2

So is it possible to define multiple return types?

Upvotes: 4

Views: 12367

Answers (4)

Niko9911
Niko9911

Reputation: 371

Updating this as of 2020.

There has been vote for Union types and it was accepted to PHP 8. https://wiki.php.net/rfc/union_types_v2

To have multiple return types in PHP you just need to have PHP 8, otherwise it is simply not possible at all. However, you should not be dumm and set everything as possible return, as it might make your code a mess. @Adam Cameron explains this very well in his answer.

Upvotes: 6

tfont
tfont

Reputation: 11233

Union types should exist, but they don't.

You can use ?int for example, but then it will expect NULL or INT.

The Answer:

It's not possible. There isn't any way natively through PHP.

However, you can omit the 'Type Declaration' which is ONLY there for strict typing. Just use DocBlocks @return bool|string for example. The IDE will recognize it, but PHP won't care.

http://docs.phpdoc.org/references/phpdoc/tags/return.html

Upvotes: 3

Adam Cameron
Adam Cameron

Reputation: 29870

@scott-c-wilson has explained the mechanics of the language rule well.

From a design perspective, having a function that potentially returns different types of result indicates a potential design flaw:

  • If you're returning your result, or otherwise false if something didn't go to plan; you should probably be throwing an exception instead. If the function processing didn't go according to plan; that's an exceptional situation: leverage the fact. I know PHP itself has a habit of returning false if things didn't work, but that's just indicative of poor design - for the same reason - in PHP.

  • If your function returns potentially different things, then it's quite possible it's doing more than one thing, which is bad design in your function. Functions should do one thing. If your function has an if/else with the true/false block handling different chunks of processing (as opposed to just handling exit situations), then this probably indicates you ought to have two functions, not one. Leave it to the calling code to decide which is the one to use.

  • If your function returns two different object types which then can be used in a similar fashion in the calling code (ie: there's no if this: do that; else do this other thing in the calling code), this could indicate you ought to be returning an interface, not a concrete implementation.

  • there will possibly be legit situations where returning different types is actually the best thing to do. If so: all good. But that's where the benefit of using a loosely typed language comes in: just don't specify the return type of the function. But... that said... the situation probably isn't legit, so work through the preceding design considerations first to determine if you really do have one of these real edge cases where returning different types is warranted.

Upvotes: 12

Scott C Wilson
Scott C Wilson

Reputation: 20016

Is it possible to define multiple return types?

No. That's the point! Multiple return types are confusing and mean you need to do extra non-intuitive checking (I'm looking at you, "===").

From the manual: "...return type declarations specify the type of the value that will be returned from a function." (emphasis mine)

The type, not the types.

Upvotes: 7

Related Questions