I want to do something like this:
I have a function like this that I don't know what exactly inside it, I just know that the function should have a parameter and one return, it may have if statement, foreach or anything. I want to get function find that parameter and return, and specify it's code:
function a($param) {
if($param > 1) {
return $param;
}
}
and I want to pass this function to another function and edit it and then call it:
function b(a($param)) {
// get function code
// change return in that function to something else like echo
// specify if statements and anything else and change to what I want
// and then call it and use it
}
Is this possible in php?
CodePudding user response:
Is this possible in php?
Yes, that is called code re-use. The idea is to re-use the existing function but not modifying it's code. You can have your function, too, thought.
Let's take the echo
example as it's that straight forward:
echo a($param);
Done.
Now that is perhaps too trivial in your case. So why not write a function your own?:
function b($param) {
$result = a($param);
// ... do whatever you want with the result ...
return $result; // finally return.
}
And then:
echo b($param); // use your function instead of the other.
This is a common way to re-use the code of functions within your own code.
If the original function is not doing what you need, take a different one that does or communicate with its author(s) to raise your issue(s).
Now what if it is not always a()
but maybe a1()
, a2()
, ..., aN()
to be used in b()
?
One way to handle this is to abstract from a()
by making it a function parameter of b()
:
function b(callable $a, $param) {
$result = $a($param);
// ... do whatever you want with the result ...
return $result; // finally return.
}
Now b()
can be called with any kind-of-a()
function and its $param
.
This might not fully suffice in your case but perhaps opens up a direction you can benefit from.
Let's see.
Perhaps the additional parameter $a
stands in the way when calling b()
.
Previously it was:
echo a($param);
Then:
echo b($param);
And it now became:
$a = 'a' . random_int(1, 99); // $a could be any-kind-of-a()
// ...
echo b($a, $param);
Now similar to having a function as a parameter to make it "dynamic", it is also possible in PHP to have it as a return value: A function that returns a function:
function b_of_a(callable $a): callable {
// b()'s original implementation (enclosing $a)
return function ($param) use ($a) {
$result = $a($param);
// ... do whatever you want with the result ...
return $result; // finally return.
};
};
Similar to a() -> $a()
there is now b() -> $b()
:
$a = 'a' . random_int(1, 99); // $a could be any-kind-of-a()
// ...
$b = b_of_a($a); // b() with any-kind-of-a() as $b
// ...
echo $b($param);
What were fixed function names in the original code have become variables you can pass around. The way both the kind-of-a()
and the kind-of-b()
function calls are preserved: with the single parameter.
And while the code of b()
is fixed, what is a()
within it, can be injected as a parameter.