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.
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.