david
david

Reputation: 181

php returning mixed data types - good or bad

When returning values in php, is it considered good or bad practice to return mixed data types. I'm working on a project where I am constantly faced with methods that return an id number or null. I'm handling the null value by checking for null and returning -1 if it is null.

Another situation I find myself in a lot is where a method should do something and return a string. But sometimes it's not possible to return the string as it wasn't found or an exception happened. What's the best thing to do here? Return a string like 'failed' or something? This then creates a string coupling between methods, I think, as the calling method has to know exactly the string failure message to check for??

EDIT: OK there are a few different opinions already. I like the idea of returning false on failure and the actual result whatever its data type is on success. But... is there a defacto best practice when it comes to this? I mean, what do programmers in other languages do i.e. java and c++ etc in these situations?

Upvotes: 18

Views: 14941

Answers (7)

j-penguin
j-penguin

Reputation: 33

This answer needs an update. There is a better solution as of PHP 7.1:

This is what I do if I want to return a string but it might be null:

    function myFunction(): ?string{
        //do things here and return string or null
    }

The ? before the string essentially means null or string as the return type. This allows you to have a limited return type but still allows null. This works for all types.

You can read more here PHP Manual - returning values

Upvotes: 0

Sliq
Sliq

Reputation: 16494

Returning mixed type is bad, at least today in 2013. Boom! The way to go is to split this:

BAD, mixed return type style:

function checkResult($data)
{
    if ($data) {
        ...
        return $stuff;
    } else {
        return false;
    } 
}

People will need additional logic to work checkRsult(), and they never know exactly what type will return.

GOOD, clearly fixed return type style:

Maybe the example is not really good, but it shows the way to go.

function doesResultExist($data)
{
    if ($data) {
        return true;
    }
    // default return
    return false;
}

function getResultData()
{
    ...
    return $stuff;   
}

Upvotes: 6

greg
greg

Reputation: 767

I agree with the answers above.

However if you design a whole system, the "best practice" would be to use exceptions: always return something meaningful, and in case of anomaly, throw an exception. The caller can then deal with the situations he knows how to face, and let somebody higher catch the rest.

Upvotes: 4

ovais.tariq
ovais.tariq

Reputation: 2625

A function that returns mixed values is not considered bad., in fact thats the beauty of php, it being a dynamic language., so the thing to do is return false on failure and the required value if the function executes correctly,.

if( false == ( $data = do_something() ) ) return false;
else print_r( $data );

Upvotes: -1

Sjoerd
Sjoerd

Reputation: 75588

Null is a fairly common return value to indicate that there is no return value. You should return null (not "failed", or -1) if the function wants to return no ID at all.

If it is exceptional that an ID was not found, you should throw an exception.

Upvotes: 7

Christa
Christa

Reputation: 485

I think it is bad practice to return mixed data types. It is possible, as you pointed out, but think about the readability and maintainability of your code. Make sure you comment what you are returning and why, I think that is going to be most important. If you are expecting back an int and you return -1 instead of null, comment that, so you (or someone else) doesn't go crazy trying to figure out what you were trying to do.

Upvotes: 4

gen_Eric
gen_Eric

Reputation: 227200

What I usually do is if the method worked, return the value, and if it failed return FALSE. That's what a lot of PHP's built-in methods do. So, then you can just check if the function returned FALSE or not.

Upvotes: 9

Related Questions