Is it possible to convert a PHP function's parameter list to an array?
-
07-07-2019 - |
Question
Suppose I have a function:
function my_function($a, $b, $c) {
...
}
I want to be able to play with my parameter list as if it were an array - as with here, using a fake $parameter_list
variable:
function my_function($a, $b, $c) {
foreach ($parameter_list as $value) {
print $value."\n";
}
...
}
How can I do this?
Solution
Just call func_get_args()
.
It's normally used for user-defined functions that have a variable number of arguments but there's no reason you can't use it for a "normal" function.
This returns a numerically-indexed array of function arguments. There is no way of returning the parameter names.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow