Array for passing PHP

I saw several people ask a similar question. But I need to clarify this question a bit.

I have several functions that pass multiple arguments. I even have a few of 10 arguments. (originally did not plan, just grew over time)

I have no problem finding the source code to find out what the 7th argument is, but it is tedious. Most of the time I know what arguments need to be passed, not a position.

I had a few ideas to simplify the process for me.

a) pass 1 argument, but split everything into some sort of delimiter. (but this is a bad idea !, since I still need to remember everyone’s position.

function myfunc('data|data|data|'){ // explode the string } 

b) pass the array with the key and values ​​and look for the key names inside my function and act accordingly.

 function myfunc(array('arg1' => 'blah blah', 'arg2' => 'more blah')){ // loop through the array } 

c) keep it as is.

 function myfunc($arg1,$arg2,$arg3,$arg4,$arg5........){ // yay } 

So, I'm looking for other options and better ideas for handling functions with growing lists of arguments.

+4
source share
9 answers

b) (associative array) is by far the easiest and most adaptable (since it allows you to simulate default keyword arguments) and the least error-prone version.

+2
source

In my opinion, the best way is to pass an associative array. This way, you can immediately see what each argument does. Just remember to name them descriptively, not arg1 and arg2 .

Another advantage of the associative array is that you do not need to worry about the order in which the arguments are passed.

Just remember that if you use an associative array, you lose your own way of assigning PHP default values, for example:

 function doSomething($arg1 = TRUE, $arg2 = 55) { } 

So, you need to create your own set of default parameters, and then combine the arrays:

 function doSomething( $props = array() ) { $props = array_merge(array( 'arg1' => TRUE, 'arg2' => 55 ), $props); // Now use the $props array } 
+8
source

go with the myfunc($options) form when you have a lot of advanced options that you can mix and match. Use array_merge_recursive with an array of default parameters and you will be golden.

+2
source

First, see if you can introduce a paramter object . It is not necessary to replace all 7 parameters, but perhaps you can reduce their number.

Next, consider the function itself - why does it need so many parameters, maybe it's just for many things? Is Replace parameter with method / Replace parameter with explicit method applicable?

By the way, Refactoring reads great!

+1
source

Depending on the intention of the function, it is sometimes advisable to pass an object.

0
source

Do not talk about decision number 1. You already say that this is bad ...

I like the second idea of ​​passing this hash associative array. This is similar to rubylike :) What you do is that you refuse some checks that you will get for free. If you call a function and forget the argument, the runtime complains and the script stops execution. In your scenario, you will have to do all these checks inside your function: are all the necessary parameters? Are values ​​the correct types, etc.?

I would say the decision depends on your taste, if only you are working on the code. If others work with you, I would ask them what I think. Until then, I would stick to the classic function approach with 7 arguments

0
source

An associative array with keys as parameter names, the order is not taken into account (do not forget about what documents are expected):

 function myFunc(array $args) { # default values $args += array('arg1' => 'default1', 'arg2' => 'default2'); } 

Sometimes you want to verify that you also remove all non-essential keys from the input. A useful function for this is array_intersect_key Documents .

0
source

check out this post about creating ellipsis in PHP.
PHP ellipsis creation

0
source

function myfunc ($ arg1, $ arg2, $ arg3, $ arg4, $ arg5 ........) {

With the limited information provided, I would have to say save it as . The reason for this is because your function signature shows that none of your arguments have default values ​​(they are all necessary).

If you create an associative array, then you remove the analyzer's ability to warn about unnamed functions, that is:

 Missing argument 2 for some_function, called in [etc.] 

IDEs can help when calling a function if you have difficulty remembering all the arguments.

Basically, if this is the required argument, it should be in the function signature.

0
source

All Articles