CakeFest 2024: The Official CakePHP Conference

ReflectionMethod::invokeArgs

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

ReflectionMethod::invokeArgs実行する

説明

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

リフレクション対象のメソッドを実行し、その引数を配列として渡します。

パラメータ

object

メソッドを実行するオブジェクト。staticメソッドを実行する場合は、このパラメータには null を渡すことができます。

args

メソッドに渡すパラメータを配列で指定します。

戻り値

メソッドの結果を返します。

エラー / 例外

メソッドが宣言されているクラスのインスタンス以外を object パラメータで指定すると ReflectionException が発生します。

メソッドの実行に失敗すると ReflectionException が発生します。

変更履歴

バージョン 説明
8.0.0 args のキーは、 静かに無視されるのではなく、パラメータの名前として解釈されるようになりました。

例1 ReflectionMethod::invokeArgs() の例

<?php
class HelloWorld {

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

}

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

上の例の出力は以下となります。

Hello Mike

注意

注意:

関数の引数の中にリファレンス渡しを要するものがある場合は、 渡す引数の中でもリファレンスにしておく必要があります。

参考

add a note

User Contributed Notes 4 notes

up
12
serg dot smertin at gmail dot com
13 years ago
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);
}
?>
up
4
agent_harris at secure-mail dot biz
13 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.
up
2
CodeCoutureXX at gmail dot com
8 years ago
If you need to call ReflectionMethod::invokeArgs() on a static function you can pass NULL in for the $object parameter.

Example:

<?php
class myClass {
public static
myStaticFunc($a, $b) {
return
$a + $b;
}
}

$ref = new ReflectionMethod('myClass', 'myStaticFunc');
echo
$ref->invokeArgs(NULL, [12, 7]);
?>

produces the following output:

19
up
0
cweiske at cweiske dot de
13 years ago
Passing arguments by reference works:
<?php $rm->invokeArgs($object, array(&$foo, $bar)); ?>
To Top