Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 376
Nate Avatar asked Sep 25 '12 20:09

Nate


People also ask

Can I pass an array to a function in PHP?

You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside. function sendemail($id, $userid){ // ... }

What is the difference between function without parameter and a function with parameter in PHP?

A function with argument, as in function(something) is to return a value after processing within the function. A function with no argument, eg function(), is to perform a task with no value returned. In this case, the function acts like a procedure.

How are parameters passed by reference different than those passed by value in PHP?

In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

What are the different ways of passing parameters?

Parameters can be passed to a method in following three ways : Value Parameters. Reference Parameters. Output Parameters.


2 Answers

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.

like image 188
Jakub Lédl Avatar answered Sep 27 '22 18:09

Jakub Lédl


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.

like image 27
JvdBerg Avatar answered Sep 27 '22 18:09

JvdBerg