php returning mixed data types - good or bad

后端 未结 7 691
梦谈多话
梦谈多话 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

提交回复
热议问题