PHP Unconference Europe 2015


(PHP 5 >= 5.1.0)

ReflectionMethod::invokeArgsInvoke args


public mixed ReflectionMethod::invokeArgs ( object $object , array $args )

Invokes the reflected method and pass its arguments as array.

Elenco dei parametri


The object to invoke the method on. In case of static methods, you can pass null to this parameter.


The parameters to be passed to the function, as an array.

Valori restituiti

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.


Example #1 ReflectionMethod::invokeArgs() example

class HelloWorld {

    public function 
sayHelloTo($name) {
'Hello ' $name;


$reflectionMethod = new ReflectionMethod('HelloWorld''sayHelloTo');
$reflectionMethod->invokeArgs(new HelloWorld(), array('Mike'));

Il precedente esempio visualizzerĂ :

Hello Mike



If the function has arguments that need to be references, then they must be references in the passed argument list.

Vedere anche:

add a note add a note

User Contributed Notes 3 notes

serg dot smertin at gmail dot com
4 years ago
We can do black magic, which is useful in templating block calls:

->__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();
$reflection->getParameters() as $param)
/* @var $param ReflectionParameter */
$pass[] = $args[$param->getName()];
$pass[] = $param->getDefaultValue();

$reflection->invokeArgs($this, $pass);
cweiske at cweiske dot de
3 years ago
Passing arguments by reference works:
<?php $rm->invokeArgs($object, array(&$foo, $bar)); ?>
agent_harris at secure-mail dot biz
3 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:

$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.
To Top