(PHP 5 >= 5.1.0)
ReflectionMethod::invokeArgs — Invoke args
Invoke arguments.
Returns the method result.
A ReflectionException if the object parameter does not contain an instance of the class that this method was declared in.
A ReflectionException if the method invocation failed.
Beispiel #1 ReflectionMethod::invokeArgs() example
<?php
class HelloWorld {
public function sayHelloTo($name) {
return 'Hello ' . $name;
}
}
$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod->invokeArgs(new HelloWorld(), array('Mike'));
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Hello Mike
We can do black magic, which is useful in templating block calls:
<?php
$object->__named('methodNameHere', array('arg3' => 'three', 'arg1' => 'one'));
...
/**
* Pass method arguments by name
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __named($method, array $args = array())
{
$reflection = new ReflectionMethod($this, $method);
$pass = array();
foreach($reflection->getParameters() as $param)
{
/* @var $param ReflectionParameter */
if(isset($args[$param->getName()]))
{
$pass[] = $args[$param->getName()];
}
else
{
$pass[] = $param->getDefaultValue();
}
}
return $reflection->invokeArgs($this, $pass);
}
?>
it seems that ReflectionMethod::invodeArgs() dont like reference-arguments in the method to call:
<?php
class Test{
function foo(&$arg){
$arg++;
}
}
$test = new Test();
$class = new ReflectionClass("Test");
$method = $class->getMethod("foo");
$bar = 9;
$method->invokeArgs($test, array($bar));
?>
RESULT: "Invocation of method Test::foo() failed"