Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Passing parameters by their names, like some languages allow.
<?php
function _f($callback,$arguments) {
if(!is_callable($callback))throw new Exception('Function '.$callback.' does not exist.');
$params = new ReflectionFunction($callback);
$params = $params->getParameters();
$argNames = array(); foreach ($params as $param){
$argNames[] = $param->name;
}
$argArray=array();
foreach($argNames as $name){
if(isset($arguments[$name]))$argArray[$name]=$arguments[$name];
else throw new Exception('Not all required parameters passed. '.$key.' is missing');
}
return call_user_func_array($callback,$argArray);
}
function test($name,$surname){
echo($name.' '.$surname.'<br/>');
}
_f('test',array('name'=>'Ilia','surname'=>'Andrienko'));
_f('test',array('surname'=>'Andrienko','name'=>'Ilia'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment