Php,invokeArgs:参数已更改,如何返回它们?
首先,我想测试一个函数:Php,invokeArgs:参数已更改,如何返回它们?,php,reflection,pass-by-reference,variadic-functions,Php,Reflection,Pass By Reference,Variadic Functions,首先,我想测试一个函数: private function testMe (array &$output) { $output['a'] = 3; // $$$$ $output gets changes } 我提出了一个小方法来公开它,并呼吁: public static function makePublicAndCall ($objectInstance, $methodname) { $ref = new ReflectionMethod (get_class(
private function testMe (array &$output)
{
$output['a'] = 3; // $$$$ $output gets changes
}
我提出了一个小方法来公开它,并呼吁:
public static function makePublicAndCall ($objectInstance, $methodname)
{
$ref = new ReflectionMethod (get_class($objectInstance), $methodname);
$ref->setAccessible(true);
$params = array();
for ($i = 2+1; $i <= func_num_args(); $i++)
{
$params[] = func_get_arg($i-1);
}
$result = $ref->invokeArgs ($objectInstance, $params);
for ($i = 2+1; $i <= func_num_args(); $i++)
{
// write back $$$$ here I would need something like "func_get_arg($i-1)"
}
return $result;
}
看到问题了吗?此方法有2个必需参数,所有其他参数都是可选的(它们转到调用的方法本身)。但是,如果这些参数发生更改,则无法恢复 适用于PHP5.6及以上版本
PHP5.6引入了可变参数,也可以通过引用接受参数
function makePublicAndCall ($objectInstance, $methodname, &...$args) { }
现在只需将由ref填充参数的$args
数组转发到$objectInstance->$methodname
function makePublicAndCall ($objectInstance, $methodname, &...$args) {
$ref = new ReflectionMethod (get_class($objectInstance), $methodname);
$ref->setAccessible(true);
return $ref->invokeArgs($objectInstance, $args);
}
makePublicAndCall($object, 'testMe', $output);
对于PHP5.4和5.5版本
不行,对不起
适用于PHP5.3及以下版本
调用时间传递引用仍然适用于这些古老的版本,所以可以随意使用它
function makePublicAndCall ($objectInstance, $methodname) {
$ref = new ReflectionMethod (get_class($objectInstance), $methodname);
$ref->setAccessible(true);
return $ref->invokeArgs ($objectInstance, $args);
}
@makePublicAndCall($object, 'testMe', &$output); // note the & here...
另外,您不必在
testMe
函数中期望引用,您可以得到一个充满引用的数组,这就足够了;您不需要通过ref获得一个充满引用的数组来操作引用。我解决了它,无论版本如何:
public static function makePublicAndCall ($objectInstance, $methodname, array &$params = array())
{
$ref = new ReflectionMethod (get_class($objectInstance), $methodname);
$ref->setAccessible(true);
return $ref->invokeArgs ($objectInstance, array(&$params));
}
电话:
::makePublicAndCall ($object, 'testMe', $output);
编辑:另一个解决方案,不考虑版本,但我打赌你会把帽子扔到地板上:
public static function makeStaticMethodPublicAndCall ($className, $method, &$params1 = null, &$params2 = null, &$params3 = null, &$params4 = null)
{
$className = new ReflectionClass($className);
$method = $className->getMethod($method);
$method->setAccessible(true);
return $method->invokeArgs (null, array(&$params1, &$params2, &$params3, &$params4));
}
.
.
.
::makeStaticMethodPublicAndCall ('MyClass', 'myMethod', $param1, $param2);
不知何故,“适用于所有PHP5+版本”的版本不起作用。。。我必须坚持使用PHP5.3,但这并不是你所要求的。这是通过ref传递数组,而不是可变参数…是的,你是对的。首先它确实解决了我的问题,但我意识到它还不够好
public static function makeStaticMethodPublicAndCall ($className, $method, &$params1 = null, &$params2 = null, &$params3 = null, &$params4 = null)
{
$className = new ReflectionClass($className);
$method = $className->getMethod($method);
$method->setAccessible(true);
return $method->invokeArgs (null, array(&$params1, &$params2, &$params3, &$params4));
}
.
.
.
::makeStaticMethodPublicAndCall ('MyClass', 'myMethod', $param1, $param2);