You can do this by overwriting the invokeArgs method and implementing the necessary functions (in your case, it looks like you want to use named arguments):
class ReflectionMethodA extends ReflectionMethod { public function invokeArgs($object, Array $args = array()) { $parameters = $this->getParameters(); foreach($parameters as &$param) { $name = $param->getName(); $param = isset($args[$name]) ? $args[$name] : $param->getDefaultValue(); } unset($param); return parent::invokeArgs($object, $parameters); } } $rm = new ReflectionMethodA('A', 'someMethod'); echo $rm->invokeArgs(new A(), array('b' => 1, 'a' => 2));
Output:
a - 2, b - 1
Edit: An improved (supporting both named and numbered arguments, as well as passing by reference) and a more flexible option (which will be used for any callback) is the next class that accepts any valid callback as a parameter in its constructor.
Using:
$rc = new ReflectedCallback(array(new A(), 'someMethod')); echo $rc->invokeArgs(array('b' => 1, 'a' => 2));
Gist
hakre source share