PHP Function Arguments - Use an array or not?

前端 未结 9 1535
挽巷
挽巷 2021-02-04 11:32

I like creating my PHP functions using key=>value pairs (arrays) as arguments instead of individual parameters.

For example, I prefer:

function useless_         


        
相关标签:
9条回答
  • 2021-02-04 12:25

    Your co-worker is crazy. It's perfectly acceptable to pass in an array as a function argument. It's prevalent in many open source applications including Symfony and Doctrine. I've always followed the 2 argument rule, if a function needs more than two arguments, OR you think it will use more than two arguments in the future, use an array. IMO this allows for the most flexibility and reduces any calling code defects which may arise if an argument is passed incorrectly.

    Sure it takes a little bit more work to extrapolate the values from the array, and you do have to account for required elements, but it does make adding features much easier, and is far better than passing 13 arguments to the function every time it needs to be called.

    Here is a snippet of code displaying the required vs optional params just to give you an idea:

    // Class will tokenize a string based on params
    public static function tokenize(array $params)
    {
        // Validate required elements
        if (!array_key_exists('value', $params)) {
            throw new Exception(sprintf('Invalid $value: %s', serialize($params)));
        }        
    
        // Localize optional elements
        $value            = $params['value'];
        $separator        = (array_key_exists('separator', $params)) ? $params['separator'] : '-';
        $urlEncode        = (array_key_exists('urlEncode', $params)) ? $params['urlEncode'] : false;
        $allowedChars     = (array_key_exists('allowedChars', $params)) ? $params['allowedChars'] : array();
        $charsToRemove    = (array_key_exists('charsToRemove', $params)) ? $params['charsToRemove'] : array();
    
    ....
    
    0 讨论(0)
  • 2021-02-04 12:27

    Your co-worker is right. Not only is it more code for the same functionality, it is harder to read and probably has lowered performance (Since you need to call isset for each param and you need to access an array to set values).

    0 讨论(0)
  • 2021-02-04 12:32

    I'm assuming you're asking whether it's A Good Thing to write all functions so that they accept only one argument, and for that argument to be an array?

    If you're the only person who's ever going to work on your code then you can do what you like. However, by passing all argument values through an array, anyone else is going to have to work harder to understand what the function does and why / how they could use it, especially if they're using an IDE with auto-complete for function names etc. They don't call it a "function signature" for nothing.

    I'd recommend that array parameters are reserved either for items where you don't know how many there will be (e.g. a series of data items), or for groups of related options / settings (which may be what's going on in the Wordpress example that you mention?).

    If you do continue with a blanket approach to array arguments then you should at least be aware of its impact on readability and take some steps to counter that issue.

    0 讨论(0)
提交回复
热议问题