PHP-call_user_function_array或Reflection类通过引用传递? [英] PHP - call_user_function_array or Reflection class pass by reference?

查看:157
本文介绍了PHP-call_user_function_array或Reflection类通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MVC框架中分发请求. 我有一个路由对象,该对象将当前URI与定义的路由进行匹配.如果匹配,则返回Route对象的实例.通过该Route对象,我可以访问匹配的控制器,方法和方法参数.

I'm trying to dispatch request in MVC framework. I have a routing object that matches current URI against defined routes. If there is a match, it returns instance of Route object. Through that Route object I can access matched controller, method and method arguments.

我可以这样使用它:

$route->getClass(); name of controller class to instantiate
$route->getMethod(); name of method to call
$route->getArgs(); array holding arguments that should be passed to method

如果需要,我还可以添加新的参数.例如,我可以将Dependency Injection Container和依赖的HTTP Request对象实例添加到args中,如下所示:

I can also add new arguments if I need to. For example, I can add Dependency Injection Container and current HTTP Request object instances to args like this:

$route->addArg('container', $container);
$route->addArg('request', $request);

现在$ route-> getArgs保留从URI提取的所有参数,还有$ container实例和$ request实例. $ container是我写的依赖项注入容器. $ request是代表当前HTTP请求的对象.

Now $route->getArgs holds all arguments fetched from URI, but also a $container instance and $request instance. $container is dependency injection container i wrote. And $request is object that represents current HTTP Request.

所以想法是实例化Router,获取当前Route,然后在每个方法/动作控制器中添加我想使用的对象.然后将该Route对象传递给Dispatcher,然后在调度请求时,我将能够在每个方法/操作控制器中使用$ container,$ request和其他参数.

So the idea is to instantiate Router, get current Route, then add objects I would like to use inside every method / action controller. And then pass that Route object to Dispatcher, and when request is dispatched, I would be able to use $container, $request and other arguments in every method / action controller.

我的问题是,当我使用博客控制器和发布方法时.默认情况下,并且总是,它应该具有$ container和$ request的实例(因为我将它们压入Route :: $ args,以及路由定义中定义的或从URI提取的任何其他参数/​​变量.

The problem I have is that when I use lets say blog controller and post method. By default, and always, it shoud have instance of $container, and $request (since I pushed them into Route::$args, plus any other argument / variable defined in routes definitions or fetched from URI.

所以当Im在我的Blog Controller中时,发布方法我希望它的行为如下:

So when Im in my Blog Controller, post method I want it to act like this:

public function post($container, $request)
{
if($request->isAjax) {
// -- Its ajax request
}

$twig = $container->getService("Twig");
}

我派发类似这样的请求:

I dispatch request something like this:

我正在使用call_user_function_array使其工作:

Im using call_user_function_array to make it work:

$app = new $controller();
call_user_function_array(array($app, $method), $args);

现在所有这些都可以正常工作,但是我有一个问题. 当我使用该后置控制器时,我必须注意后置方法参数的顺序.

Now this all works as it should but I have one problem. When Im using that post controller I must be careful in what order are post method arguments ordered.

所以我可以这样做:

public function post($id, $url, $container, $request) {
// -- SNIP --
}

但是我不能这样使用它:

but I cant use it this way:

public function post($container, $url, $request, $id) {
/-- SNIP --
}

所以您遇到了问题,我无法按自己的方式在方法中混合参数,并且需要注意在Route:$ args数组中定义这些参数的顺序.

So you get the problem, I cant mix arguments inside my method the way I want, and I need to keep an eye on order in which those arguments get defined in Route:$args array.

每个方法/动作控制器都应具有$ request,$ container实例以及通过URI提取或在路由定义中定义的参数.

Every method / action controller should have $request, and $container instances plus arguments fetched through URI or defined in route definition.

有什么方法可以使它工作而不必考虑post方法以什么顺序获取参数?这与通过引用传递有关吗?由于php通过值传递变量?

Is there any way to make it work without having to think In what order is post method getting arguments? Does this have something to do with passing by reference? Since php passes variables by value?

我正在使用call_user_function_array,我想找到针对call_user_function_array的解决方案. 这可能吗?

Im using call_user_function_array and I want to find solution for call_user_function_array. Is this possible?

如果没有,可以使用Reflection类完成吗?

If not, can this be done using Reflection class?

推荐答案

使用Reflection类,您可以获取要调用的函数的每个参数的参数名称,从而可以很容易地检查它们的顺序是否正确. 您不能简单地通过call_user_function_array来做到这一点.

With Reflection class you can get the arguments names of each parameter of the function you want to call and in that way check if they are in the correct order very easily. You can't do that simply with call_user_function_array.

尤其是,您可以在<一个href ="http://www.php.net/manual/zh/class.reflectionmethod.php" rel ="nofollow"> ReflectionMethod类.

从那以后,我认为您知道该怎么做(用传递的参数的键检查parameter数组的每个键,然后就可以了.)

From there on I think you know what to do (check each key of the parameters array with the key of the argument passed and there you go).

这篇关于PHP-call_user_function_array或Reflection类通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆