Passing array of parameters vs. individual parameters to a function in PHP? [closed]

元气小坏坏 提交于 2019-12-08 17:37:47

问题


When there is a function that requires a number of parameters to be passed, such as seven parameters, which is the better practice:

function foo($par1, $par2, $par3, $par4, $par5, $par6, $par7)
{
}

or

function foo(array $args)
{
}

Where in the second example $args would an array with the parameters as elements.

I have been inconsistently using both methods.

The disadvantage to using the first method is that if you get order of a parameter wrong, you're screwed (and it could be really hard to debug because it's not always obvious). This is the advantage of using the array method.

The advantage (IMHO) of using the first type is that a good PHP editor such as PhpStorm will show you the names of the parameters as you are typing the function, which is easier than going to the function to see what parameters need to be passed in an array.

Appreciate your advice.


回答1:


Theoretically, each parameter should encompass a logical whole. Collections are meant to represent a collection of values, not an aggregate of method parameters; the individual parameters should be passed separately to clearly indicate the purpose of the method.

Practically, when passing parameters in an array, you have to do a ton of manual checks to ensure that correct parameters have been passed. You can avoid pretty much any ambiguity by giving your parameters clear, logical names (and typehints where possible). Also, as a comment already stated, if a method takes 7 parameters, it's probably ripe for some refactoring.

Edit: if your method/function does accept a set of vaguely defined "options" that affect what the code does only in a minor way, consider using the Symfony OptionsResolver component, to greatly ease the validation of your options array.




回答2:


I think this question boils down to "How strong typed is php?" or "How strong typed do I want my stuff to be?"

Obvious, the array is the most loosely coupled. The number of variables and their names is complete up to the caller.

Your call with the separate parameters is a little bit more strong coupled, so there must be at least the numbers of parameters be provided.

IMHO you forgot one type of calling, and that is with type hinting. That is the most strongest way of strong typing that php, at this moment, provides.

example:

function foo(\MyNamespace\MyObject $par1, \YourNamespace\YourObject $par2)
{
}

My opinion: with my background in only strong typed languages, I prefer strong typed typed hinting. There are rumours that php 5.5 or php 6 will also support scalars as a type hint.




回答3:


If you have a lot of options based off arguments or want more flexibility try func_get_args().

function foo()
{
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
}

If you want to narrow down acceptable arguments then defining variables is best.



来源:https://stackoverflow.com/questions/12590860/passing-array-of-parameters-vs-individual-parameters-to-a-function-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!