Skip to content

Instantly share code, notes, and snippets.

@andrienko
Created July 21, 2014 17:17
Show Gist options
  • Save andrienko/d021ff2f06d618110ce4 to your computer and use it in GitHub Desktop.
Save andrienko/d021ff2f06d618110ce4 to your computer and use it in GitHub Desktop.
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