I want to call an anonymous function (lambda or closure) which has some arguments, I know the argument names but I don\'t know their order! The call_user_func_array() functi
You have to pass the parameters positionally, names aren't considered at all. You can map the parameters by name to their position using reflection:
$params = array("a" => 1, "b" => 2, "c" => 3);
$ref = new ReflectionFunction($function);
$arguments = array_map(
function (ReflectionParameter $param) use ($params) {
if (isset($params[$param->getName()])) {
return $params[$param->getName()];
}
if ($param->isOptional()) {
return $param->getDefaultValue();
}
throw new InvalidArgumentException('Missing parameter ' . $param->getName());
},
$ref->getParameters()
);
$ref->invokeArgs($arguments);