我可以将关联数组作为参数传递给 ReflectionMethod::invokeArgs 吗?
是否可以在 ReflectionMethod::invokeArgs
中将参数作为关联数组传递?这将允许我以与声明不同的顺序传递参数。
例如:
class A
{
public function someMethod($a, $b)
{
return sprintf("a - %s, b - %s", $a, $b);
}
}
$rm = new ReflectionMethod('A', 'someMethod');
echo $rm->invokeArgs(new A(), array('b' => 1, 'a' => 2));
Is it possible to pass parameters as an associative array in ReflectionMethod::invokeArgs
? This would allow me to pass the arguments in a different order than declared.
For example:
class A
{
public function someMethod($a, $b)
{
return sprintf("a - %s, b - %s", $a, $b);
}
}
$rm = new ReflectionMethod('A', 'someMethod');
echo $rm->invokeArgs(new A(), array('b' => 1, 'a' => 2));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过覆盖
invokeArgs
方法并实现您需要的功能来做到这一点(在您的情况下,您似乎想要使用命名参数):输出:
编辑:改进(支持命名和编号参数以及通过引用传递)和更灵活的变体(用于任何回调)是以下类,将任何有效的回调作为其构造函数中的参数。
用法:
要点
You can do this by overwriting the
invokeArgs
method and implementing the functionality you need (in your case it looks like you want to use named arguments):Output:
Edit: An improved (supporting both named and numbered arguments as well as passing by reference) and more flexible variant (to be used for any callback) is the following class, taking any valid callback as parameter in it's constructor.
Usage:
Gist
不,手册页中没有任何内容建议您可以使用关联数组按名称重新排序参数。
No, there's nothing in the manual page that suggests you can use an associative array to re-order arguments by name.