As suggested, but not implemented: you can use to collect parameter names, and then associate them with values: Reflection
function args_to_assoc_array($function, $args) {
if (false === strpos($function, '::')) {
$reflection = new ReflectionFunction($function);
} else {
$reflection = new ReflectionMethod(...explode('::', $function));
}
$assoc = [];
foreach ($reflection->getParameters() as $i => $parameter) {
$assoc[$parameter->getName()] = $args[$i];
}
return $assoc;
}
You can then call it the same way, whether in a function or method:
function f($x, $y) { return args_to_assoc_array(__METHOD__, func_get_args()); }
class A {
function m($z) { return args_to_assoc_array(__METHOD__, func_get_args()); }
}
var_dump(f(7, 2), (new A)->m(4));
what conclusions :
array(2) {
["x"]=>
int(7)
["y"]=>
int(2)
}
array(1) {
["z"]=>
int(4)
}
I did not count this, but I strongly suspect that it is much slower than using compact.
source
share