1

I have 2 or more functions that always takes the same arguments. The argument is the returned value of another function call.

This is the code:

$result = getMyResult();

myFunction1($result);
myFunction2($result);
...

Question

Is there a way to call multiple functions on the same line with the same argument?

An example of what I'm trying to achieve:

myFunction1,myFunction2...(getMyResult());

Demands

  • The solution can be procedural or object oriented.
  • I don't want to temporary store the returned value of getMyResult() in a variable.
  • I only want to call getMyResult() once.
  • I don't want to wrap the function calls in a helper function *

* like this.

function myHelperFunction($result) {
    myFunction1($result);
    myFunction2($result);
    ...
}

If myHelperFunction() is the closest solution, then I'm happy to hear about it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jeppsen
  • 487
  • 3
  • 10

2 Answers2

1

I've done some more research and the closest I´ve been able to come to an answer that lives up to my requirements is with 1: an object oriented approach and fluent setters or 2: with a helper function.

(Found Solution 1 here: Call multiple methods on object?)

Solution 1 (3/4 requirements) Make a class with methods and have every method return the object itself.

$result = getMyResult();
$myclass->myMethod1($result)->myMethod2($result)->...

The only "problem" is that I have to store the argument in a temp variable because I don't want to call getMyResult() more than once.

Solution 2 (3/4 requirements) Helper function procedural solution.

function myHelperFunction($result) {
    myFunction1($result);
    myFunction2($result);
    ...
}

myHelperFunction(getMyResult());

The only "problem" is that I have to define a function that calls all my other functions. This function is not dynamic because I can not control the number of functions called inside without implemententing further logic.

Conclusion

I can't find any solution that meets all my reqiurements and I can't determine if there is another solution out there at this point. Both Solution 1 and 2 would make designing my code easier. For now I will settle with Solution 1.

Jeppsen
  • 487
  • 3
  • 10
0

Interesting that this is still hanging around without a really satisfactory answer. Here is a function that will call any number of functions with the same argument.

It's a pretty common pattern.

It's not a one liner, but once you write it, you never need to again (no need for a helper function each time). Prior to PHP 7.4, it looks like this:

function for_effect() {
  $args = func_get_args();
  return function() use($args) {
          $inner_args = func_get_args();
          array_walk($args, function($fn) use ($inner_args){call_user_func_array($fn, $inner_args);});
  };
}
 
$print_me = function() {
  var_dump( func_get_args());
};

for_effect($print_me, $print_me)('Some argument');

Post 7.4, it's a bit nicer:

function for_effect() {
    $args = func_get_args();
    return function() use($args) {
        $inner_args = func_get_args();
        array_walk($args, fn($fn)=>call_user_func_array($fn, $inner_args));
    };
}
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86