php returning mixed data types - good or bad

后端 未结 7 687
梦谈多话
梦谈多话 2021-02-02 14:34

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 a

相关标签:
7条回答
  • 2021-02-02 14:55

    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

    0 讨论(0)
  • 2021-02-02 14:56

    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 );
    
    0 讨论(0)
  • 2021-02-02 14:59

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-02 15:11

    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.

    0 讨论(0)
  • 2021-02-02 15:11

    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;   
    }
    
    0 讨论(0)
提交回复
热议问题