0

Can please someone help me with this peace of code, I am completely lost. I am using foreign program and I need to run function MainFunc() once with unlimited parameter amount. There are two parameter types: "input" and "output", therefore "input" is a function with two parameters. Functions "input" and "output" are implemented elsewhere and does not interest me.

$var->MainFunc()
        ->input(1,2)
        ->output(3)
        ->input(4,5)
        ->output(6)
        ->input(7,8)
        ->output(9)

I can hardcode defined parameters, but I want to take them from other source (DataBase) and there can be many groups (input and output within each group).

Can someone, please, explain me how does this PHP syntax construction works and how can I transform it to pass unlimited parameter amount?

4
  • possible duplicate of Can a PHP function accept an unlimited number of parameters? Commented Jul 7, 2014 at 18:03
  • @cmorrissey it really isn't a duplicate considering that it is about calling chained methods and not a single call to a function. Commented Jul 7, 2014 at 18:11
  • This is not an exact problem but a how do i code something question. This is outside the scope of the site. Commented Jul 7, 2014 at 18:20
  • @JonathanKuhn I was responding to the "how can I transform it to pass unlimited parameter amount" as that seemed to be the root of his question Commented Jul 7, 2014 at 19:34

2 Answers 2

1

MainFunc, input and output just return an instance of an object. You could very easily just call each of those one time, store the return in a variable and make the next call on the next line.

For example, this is exactly the same thing:

$tmp = $var->MainFunc();
$tmp = $tmp->input(1,2);
$tmp = $tmp->output(3);
$tmp = $tmp->input(4,5);
$tmp = $tmp->output(6);
$tmp = $tmp->input(7,8);
$tmp = $tmp->output(9);

What this means is that you could easily put it in a loop and call input/output on each iteration.

Something like this would also product the same result:

//array of calls. Could be pulled/generated from db
$args = array(
    array(1,2),
    array(3),
    array(4,5),
    array(6),
    array(7,8),
    array(9)
);

//counter for even/odd (input/output)
$cnt = 0;

//the first call
$tmp = $var->MainFunc();

//loop over the arguments array
foreach($args as $argList){
    $call = array($tmp);
    //change method based on number of arguments
    if($cnt % 2 == 0){
        //even, call input
        $call[] = 'input';
    } else {
        //odd, call output
        $call[] = 'output';
    }

    //call the method
    $tmp = call_user_func_array($call, $argList);

    //increment counter
    $cnt++;
}

Edit: forgot to increment counter.

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thanks. Additional question if I may: how can I loop thou arguments if "input" function parameter amount is also flexible(minimum 1, maximum ..)? I am definitely sure that functions goes one by one: "input" than "output", than again "input" than "output".
If they alternate, then you can do something like check the key for even/odd and call either function. Then use call_user_func_array to call the method with any number of arguments. I'll update my loop example.
0

All functions return probably $this so you can use method chaining to run this code.

This code works the same as:

    $var->MainFunc();
    $var->input(1,2);
    $var->output(3);
    $var->input(4,5);
    $var->output(6);
    $var->input(7,8);
    $var->output(9);

As you see this is not connected to using unlimited parameter amount. Each time input method has 2 parameters and output method 1.

As I understood your problem you can do something like that;

$input = array(array(1,2), array(2,3), array(5,6));
$output = array(1,6,7);

$var->MainFunc();

for ($i=0, $c = count($input); $i<$c; ++$i) {
  $var->input($input[$i][0], $input[$i][1]);
  $var->output($output[$i]);
}

Of course those $input and $output array you can create taking data from database. I've just put sample data to them.

2 Comments

It is possible that the methods don't return $this but instead is a factory that returns an instance of another object. In which case you would probably want to capture the return from each method and call the next function on that.
@JonathanKuhn You are right, we don't really know what this functions return for sure. I've added probably to my answer

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.