12

Occasionally I'll write a PHP function with a single input, an associative array containing all of that function's inputs. This has benefits such as not having to remember the correct order of inputs, but I've also noticed it makes implementing changes to large codebases much easier; when I need to add another variable, and that variable has to pass through 4 or 5 pre-existing functions, it's much easier when I can just stick it in the array and pass it along.

My question is, is there a downside to doing this?

I rarely see functions written this way in examples, or in open source I use, which leads me to believe there is probably a downside. If there isn't a reason not to do it then why not write all functions this way?

UPDATE

Thanks for all your answers. It looks like two major problems stand out:

Code readability - impossible to tell what variables are going into a function and what they're for

Variable creep - Could wind up with massive arrays bouncing from one function to the next; one should not pass parameters to functions that don't require them

Which are both great points I did not think about.

It seems the general gist as well is that code where this is an issue, should probably be converted to a class. Unfortunately in this specific project such a refactoring is beyond the scope, but I also think Bill Karwin's solution is good - pass an array of optional variables

4
  • Out of interest can you post an example function you would write? :) Commented Jun 15, 2009 at 17:08
  • @Shadi - more like "out of morbid curiosity"? :) Commented Jun 15, 2009 at 17:20
  • 1
    So instead of function myFunc($var1, $var2, $var3, $var4 ...) {} you'd have function myFunc($params) { } and then reference via $params['var1'], etc Commented Jun 15, 2009 at 17:39
  • If you're curious, the concept of "named parameters" has been discussed several times by the PHP internals team, but it was ultimately rejected. Commented Dec 14, 2011 at 1:16

13 Answers 13

13

why not write all functions this way?

For that matter, why not forget about parameters completely, and use global variables for everything? (kidding)

Passing an associative array has one useful advantage: you can make multiple function parameters optional, and you can pass a value for the Nth parameter without having to pass a value for the *N-1*th parameter.

But you have no way to make mandatory parameters with a compile-time error if you don't pass them. Neither can you declare type-checking.

You'll have to write code inside the called function to check for the presence and the type of required parameters.

An alternative I have used is to declare conventional parameters for those that are mandatory, and then as the last (optional) argument, declare an associative array called $options that contains only the optional items.

function database_connect($dbname, $user, $password, array $options = array())
Sign up to request clarification or add additional context in comments.

5 Comments

Neither can you declare type-checking -- Could you please explain more on how to do this? For example, in your sample function, can we type-check $password to be of string in the function declaration? I assume you meant to type-check it inside the function itself, right?
I meant type-checking for array or class types, declared in the argument list (when I wrote this in 2009, PHP didn't have similar type-checks in the argument list for scalar types like string, int, float). If you use an $options array, you have to do all type-checking manually in your function; you can't do it declaratively.
Got it, thank you. PHP is still a weak-type language until now, no? I mean you can't specify the type of the variable e.g. int, float, etc.
@Bill Karwin, I need to create a function for uploadung picture, but I don't know how to have a input variable for function(), for example $_FILES['file']['error']
@Kermani, I don't understand what you want to do. I suggest you ask a new question, and describe what you want to achieve, what you tried so far, and how it's not doing what you want.
9

The downside is that anyone looking at your code (other than you) would have no idea what parameters are coming into a method, where they came from, or what they are for. They would also have no idea how to call a method since it isn't clear without looking at the code exactly what is required inside that "parameter array".

1 Comment

This is true, only if the coder doesn't comment or document anything.
2

presumably one of the downsides is that you'll end up with way too many parameters. This leads to the problem that the code is likely to lose it's readability.

In a strict Object-Oriented approach you would convert all these parameters that are "passed through" multiple function-calls into instance-variables of the classes holding the methods.

You should never pass parameters to functions that are not actually required by them.

1 Comment

Although if you had function foo($a, $b) { ... } and only $b mattered from the caller's point of view, you could potentially end up passing an unnecessary value as the first parameter in order to conform with the function requirement.
2

This isn't necessarily that bad of an idea, since PHP lacks the "keyword arguments" feature found in many other modern programming languages (Python, Ruby, etc.). However, there are definitely a few problems with it:

  1. If you change the parameters that a function accepts, then it's likely you'll have to change some code where it's called anyway, to match the new parameters.

  2. If you're using these arrays extensively, and in many function calls taking an array and simply "passing it along," that might be a sign that you should think about turning some of those parameters into a structured class.

  3. If you're using these arrays as a mutable data structure, you don't necessarily know that the array you have after calling the function is the same as the one you passed in. This will probably come back to get you at some point.

Comments

1

Listing each parameter can increase code readability. Passing a single associative array wouldn't necessarily explain what kind(s) of data are being passed to a function.

Comments

1

If I were to pick up your function and see that it simply starts referencing hash values within a single function argument, I would be a bit confused. Explicitly stating the variables and giving a bit of documentation within a docblock using @param helps tremendously. Having to add an argument in two places is a minor cost compared to the readability listing arguments yields.

Comments

1

I personally use the following method:

  1. declare the parameter as an array in the function definition;

  2. establish a "default values" assoc. array;

  3. match passed params with the default into a definitive parameters assoc.array that I use inside the function. For this purpose I use the param_default() function.

By following this method I avoid mandatory params issues and manage default values; also the ability to pass keys in any order is very comfortable.

// default values fx.
function param_default( $def = array(), $parameters= array() ){
    $parm_diff = array();
    $ak=array();
    $va=array();
    $parm = array();
    foreach($def as $key=>$values) if(!in_array($key, array_keys($parameters))){
        $ak[]=$key;
        $va[]=$values;
    }
    $parm_diff = array_combine($ak,$va);
    $parm = array_merge($parm_diff,$parameters);
    unset($parm_diff, $ak,$va);

    return $parm;
}

So this is a simple example of usage:

<?php
// sample use 
 function my_func( $parameters = array() ){
    $def = array(
        'first' => 1,
        'second' => 'foo',
        'third' => -1
     );
   $parm = param_default( $def, $parameters );
   //hereon I can use my array

   var_dump($param);

   if ($param['second'] !=='foo'){
       echo 'Not foo!!';
       // whatever...
   }

   return true;
}
?>

Example of output:

 // calling my_fun()

$example = my_func( array('second'=>'bar') );

// results in 
$param array: 
'first' => 1,
'second' => 'bar',
'third' => -1

Comments

0

The biggest code smell here is that adding another variable would cascade through 4-5 different functions. I can't really think of a reason this would need to happen.

It sounds like you need to refactor this code into a class so that then you can pass the values into a constructor one time, and save them as member variables.

Comments

0

It makes it a lot easier to generate documentation if you have the parameters named and passed in separately. You might think using an array makes it easier to add new code, but the upside there doesn't beat out readability. Plus go away from your code for 2 months and come back...figuring out in what order to add params to your array in which situations will become a massive headache.

Comments

0

In some cases this does make sense I feel, but often you can simplify things by creating multiple methods with slightly different behaviours. The name of the method will then make it clear what it's for.

In general if your functions have > 3 arguments it will become unreadable anyway.

As an alternative, and often used pattern is making the single argument of your function a (classed-)object. The 'parameter class' will define exactly what's supported.

Comments

0

If someone's still searching for a solution - here it is (example-function is __construct() ):

Extract $_GET/$_POST params and pass them to constructor

Comments

0

if your quetion is that how to give an array as inputof function, this is your Answer:

<?php
 function city($input){
   $i=count($input['iran']);
   $result="";
     for($b=1;$b<=$i;$b++){
       $result.='<div style="background:gold">'.$input['iran'][$b].'</div>';
       }
      return $result;
    }
$cityName['iran'][1]="Tehran";
$cityName['iran'][2]="Qom";
$cityName['iran'][3]="MashHad";
$cityName['iran'][4]="Isfahan";
$cityName['iran'][5]="Shiraz";
$cityName['iran'][6]="Yazd";

echo city($cityName);
?>

Comments

0

Create the function:

function testing($argumentos) {
    $var_1 = $argumentos['var_01'];
    $var_2 = $argumentos['var_02'];

    echo $var_1 . " " . $var_2;
} 

Call the function:

testing(array('var_01' => "mierda", 'var_02' => "conio!"));

Output:

 mierda conio!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.