Skip to content

Instantly share code, notes, and snippets.

@x7c1
Created September 12, 2011 16:49
Show Gist options
  • Select an option

  • Save x7c1/1211744 to your computer and use it in GitHub Desktop.

Select an option

Save x7c1/1211744 to your computer and use it in GitHub Desktop.
named parameters in php
<?
namespace closure;
class name {
public static function parameter($f){
$ref = new \ReflectionFunction($f);
$underlying = [];
foreach($ref->getParameters() as $param){
$underlying[$param->getName()] = null;
}
return function($args) use($f, $underlying){
$values = array_merge($underlying, $args);
return call_user_func_array($f, array_values($values));
};
}
}
<?
include_once 'closure.php';
use closure\name;
class NamedParameterTest extends PHPUnit_Framework_TestCase{
public function test_named_parameter(){
$increment = name::parameter(function($x, $y, $z){
return [$x+1, $y+1, $z+1];
});
$values = $increment([
'z' => 2,
'y' => 1,
'x' => 0,
]);
$this->assertSame([1,2,3], $values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment