2

在 PHP 中将变量作为数组还是作为单独的变量传递是否更好,

我的意思是在 30 个关联数组中,我可能使用其中的 5 个变量?将它们作为五个变量单独传递,还是将整个数组传递过来会更好吗?

4

1 回答 1

3

In generic terms, there are some things to consider when it comes to choosing how to define your functions. This may not directly answer your question, but it may provide a rationale for choosing one way over another, and why they may be better for different things.

function do_something($a, $b, $c, $d, $e) {
    ...
}

function do_something(array $a) {
    ...
}

First and foremost, there are the semantics. Does your function depend on that particular array, or is it just that the arguments you would pass happen to be in an array in that case?

If it is dependent on the array, then it would stand to reason to pass that in as the sole argument, and the code remains readable, because you wouldn't use it in any other case:

function do_something_to_big_array($big_array) {
    ....
}

If it isn't that specific, you may have problems.

The function becomes obscure: You can't use it without reading the function to find out what elements the array must contain.

The function becomes more complex: You can supply a default array with default values in the definition, but that won't be used when you supply the argument. You'll find yourself trying to re-implement a standard argument list, checking if keys exist, setting them if they don't, etc.

// default array if no argument supplied
function do_something($a = array('a' => true, 'b' => 10, 'c' => false)) {
    // you don't know if you're working with the default or not
    if (!isset($a['c'])) $a['c'] = false;
    ...
}

// define a defaults array and replace duplicates with $a's values
function do_something(array $a) {
    $defaults = array('a' => true, 'b' => 10, 'c' => false);
    $a = array_replace($defaults, $a);
}

// have defaults without extra logic in the function
function do_something($a = true, $b = 10, $c = false) {
   ...
}

Imagine having to do that for every function you declare, so you can pass in an array and have it work reliably, instead of separate arguments.

Readability suffers; more clutter: You have all the data you need to pass in to the function, but you have to create an associative array containing that data before you can use it.

$data = array('a' => $a, 'b' => $b, 'c' => $c);
do_something($data);

Of course, there is always an exception, and you might find yourself with a large arguments list:

function do_something($a, $b, $c, ... $x, $y, $z) {
   ...
}

That's unreasonable and needs refactoring, so you can either pass in an array, and work with that, because you're never going to use it in any other case:

function do_something_with_alphabet(array $alphabet) {
    ...
}

Or maybe you could split it into more functions if it was reasonable to do so:

function do_something_first($a, $b, $c) {
    ...
}

function do_something_last($x, $y, $z) {
    ...
}

There's no point saying you have to do it this way, or that way, or even the other way, but it's worth keeping it in mind because you may write a function in one situation, and then you (or someone else) may re-visit it many moons later, and have trouble figuring out what is going on. If it makes absolute sense to pass a single array to a function, then that is it. If it doesn't, and it's for convenience, then think about it a bit more.

于 2012-04-07T13:30:38.333 回答