Created
July 21, 2014 17:17
-
-
Save andrienko/d021ff2f06d618110ce4 to your computer and use it in GitHub Desktop.
Passing parameters by their names, like some languages allow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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