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);
}
?>
ReflectionMethod::invokeArgs
(PHP 5 >= 5.1.0)
ReflectionMethod::invokeArgs — Invoca un método con argumentos
Descripción
Invoca el método reflejado y pasa sus argumentos como array.
Parámetros
Valores devueltos
Devuelve el resultado del método.
Errores/Excepciones
Lanza ReflectionException si el parámetro object
no contiene una instancia de la clase en la que está declarado este método.
Lanza ReflectionException si falla la invocación al método.
Ejemplos
Ejemplo #1 Ejemplo de ReflectionMethod::invokeArgs()
<?php
class HolaMundo {
public function saludarA($nombre) {
return 'Hola ' . $nombre;
}
}
$metodoReflexionado = new ReflectionMethod('HolaMundo', 'saludarA');
echo $metodoReflexionado->invokeArgs(new HolaMundo(), array('Miguel'));
?>
El resultado del ejemplo sería:
Hola Miguel
Notas
Nota:
Si la función tiene argumentos que necesitan ser referencias, éstos deben ser referencias en la lista de argumentos pasados.
Ver también
- ReflectionMethod::invoke() - Invoca un método
- __invoke()
- call_user_func_array() - Llamar a una llamada de retorno un array de parámetros
serg dot smertin at gmail dot com ¶
2 years ago
cweiske at cweiske dot de ¶
2 years ago
Passing arguments by reference works:
<?php $rm->invokeArgs($object, array(&$foo, $bar)); ?>
agent_harris at secure-mail dot biz ¶
2 years ago
There is a simple workaround for the reference passing problem:
Since the reflection api has to handle all parameters in a generic way it has no chance to guess if you wish to pass data per value or reference.
But it seems that you can also decide to pass a reference from where you call the function or method (not just only by the ampersand prefix in its declaration).
So just do the following; which worked for me:
<?php
//...
$method->invoke($object, $inputValue, &$outputValue);
?>
Since this will only be necessary with arrays and primitive data types it should be acceptable in most cases to know in advance if you need to pass per reference. But it is probably although necessary to keep the ampersand always in the declaration (because of the at least two layers between the actual function and your invoke call).
If this is the expected behavior it will maybe make sense to mention it in the documentation for invoke and invokeArgs.
addiks at gmx dot de ¶
3 years ago
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"
