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
getMyResult()
in a variable.getMyResult()
once.* like this.
function myHelperFunction($result) {
myFunction1($result);
myFunction2($result);
...
}
If myHelperFunction()
is the closest solution, then I'm happy to hear about it.
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.