In php, should I return false, null, or an empty array in a method that would usually return an array?

后端 未结 8 598
面向向阳花
面向向阳花 2021-02-01 18:22

I\'ve found several responses to this, but none pertaining to PHP (which is an extremely weak typed language):

With regards to PHP, is it appropriate to return

8条回答
  •  旧巷少年郎
    2021-02-01 19:13

    Here's a modern answer that's been valid since the 1960's probably.

    Some bad design choices in the earliest versions of PHP (before PHP 4) have made many PHP developers exposed to conventions that have always been bad. Fortunately, PHP 5 have come and gone - which helped guide many PHP developers on to the "right path".

    PHP 7 is now seeing the benefits of having been through the PHP 5 phase - it is one of the fastest performing script languages in existence.

    • and this has made it possible to make PHP 7 one of the fastest and most powerful scripting languages in existence.

    Since PHP version 4, huge efforts have been made by PHP core developers to gradually improve the PHP language. Many things remain, because we still want to have some backward compatability.

    DON'T return false on error

    The only time you can return FALSE in case of error, is if your function is named something like isEverythingFine().

    false has always been the wrong value to return for errors. The reason you still see it in PHP documentation, is because of backward compatability.

    1. It would be inconsistent. What do you return on error in those cases where your function is supposed to return a boolean true or false?

    2. If your function is supposed to return something other than booleans, then you force yourself to write code to handle type checking. Since many people don't do type checking, the PHP opcode compiler is also forced to write opcodes that does type checking as well. You get double type checking!

    You may return null

    Most scripting languages have made efficient provisions for the null value in their data types. Ideally, you don't even use that value type - but if you can't throw an exception, then I would prefer null. It is a valid "value" for all data types in PHP - even if it is not a valid value internally in the PC.

    Optimally for the computer/CPU is that the entire value is located in a single 1, 2, 4 or 8 byte memory "cell". These value sizes are common for all native value types.

    When values are allowed to be null, then this must be encoded in a separate memory cell and whenever the computer needs to pass values to a function or return them, it must return two values. One containing isNull and another for the value.

    You may return a special value depending on type

    This is not ideal, because

    • If your function is supposed to return an integer, then return -1.
    • If your function is supposed to return a string

    You should throw exceptions

    Exceptions match the inner workings of most CPUs. They have a dedicated internal flag to declare that an exceptional event occurred.

    It is highly efficient, and even if it wasn't we have gigantic benefits of not having a lot of extra work in the normal non-erroneous situation.

提交回复
热议问题