从路由器获取计数控制器参数?
好的,我正在学习如何制作自己的自定义 MVC 系统,到目前为止我已经创建了路由器和控制器之类的东西。
例如,我有一个控制器配置文件类和一个网址 www.helloworld.com/args1/args2/args3
class Profiles
{
function index($args1,$args2)
{
echo var_dump($args1,$args2);
}
}
这是我的路由器类的一部分,它执行该方法和参数,
if (is_callable(array($controller,$method))) {
call_user_func_array(array($controller, $method), $this->params);
}
到目前为止没有错误,但不知何故我需要给出 404。如果$this->params
与我在 call_user_func_array(array($controller, $method), 调用的方法中的
count(params)
不同 $this->params);
好吧,我的想法是,如果 count get params 与被调用类的 count params 不同,则 404, 可以说我可以计算我的 $this->params 它给了我一个 3。但是被调用类中的参数怎么样? 我们如何通过call_user_func_array计算被调用类中的参数?
感谢您关注
亚当·拉马丹
ok im learning on how to make my own custom mvc system, so far i have created things like router and the controller.
example i have a controller profile class and an url www.helloworld.com/args1/args2/args3
class Profiles
{
function index($args1,$args2)
{
echo var_dump($args1,$args2);
}
}
heres are part of my router class that execute the method and the parms
if (is_callable(array($controller,$method))) {
call_user_func_array(array($controller, $method), $this->params);
}
so far no error but somehow i need to give a 404. if the $this->params
is different then the count(params)
in the method that i call at call_user_func_array(array($controller, $method),
$this->params);
ok what im thinking is if count get params not same as count params at the called class then 404,
lets say i can count my $this->params it gave me a 3. but how about the params in the called class ?
how can we count params in a called class by call_user_func_array?
Thanks for looking in
Adam Ramadhan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上相当简单。您可以通过使用 func_get_args 将参数提取到数组中来确定参数的数量,然后使用 计数 它们。例子:
Rather simple, actually. You can determine the amount of parameters by fetching them into an array using func_get_args, and then count them. Example:
您可以使用反射:
可以通过检查参数是否过多来变得更严格。
You could use reflection:
Can be made stricter by checking if there are too many parameters.
据我所知,如果不使用 reflection 就无法获取方法的参数计数。然而,它非常简单,看起来像这样:
Will Yield
请注意,反射总是伴随着成本。然而,这对性能的影响应该不明显。
From what I know, there is no way of getting the parameter count for a method without using reflection. However, it's pretty simple and would look something like this:
Will yield
Note that reflection always comes with a cost. However, the performance implications of this should not be noticable.