ReflectionMethod
在线手册:中文 英文
PHP手册

ReflectionMethod::invokeArgs

(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.

参数

object

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

args

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

返回值

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

<?php
class HelloWorld {

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

}

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

以上例程会输出:

Hello Mike

注释

Note:

如果函数有参数需为引用, 那么它们必须以引用方式传入.

参见


ReflectionMethod
在线手册:中文 英文
PHP手册
PHP手册 - N: Invoke args

用户评论:

cweiske at cweiske dot de (25-Mar-2011 03:34)

Passing arguments by reference works:
<?php $rm->invokeArgs($object, array(&$foo, $bar)); ?>

agent_harris at secure-mail dot biz (13-Feb-2011 03:12)

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.

serg dot smertin at gmail dot com (21-Sep-2010 02:51)

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);
      }
?>

addiks at gmx dot de (20-May-2010 04:14)

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"