从路由器获取计数控制器参数?

发布于 2024-10-13 01:18:24 字数 849 浏览 2 评论 0原文

好的,我正在学习如何制作自己的自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥琐帝 2024-10-20 01:18:36

通过call_user_func_array计算被调用类中的参数

实际上相当简单。您可以通过使用 func_get_args 将参数提取到数组中来确定参数的数量,然后使用 计数 它们。例子:

<?php
function foo( ) {
   echo "I have received " . count( func_get_args( ) ) . " parameters.";
}
foo( ); // I have received 0 parameters.
foo( 1, 2, 3, 4, 5 ); // I have received 5 parameters.

count params in a called class by call_user_func_array

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:

<?php
function foo( ) {
   echo "I have received " . count( func_get_args( ) ) . " parameters.";
}
foo( ); // I have received 0 parameters.
foo( 1, 2, 3, 4, 5 ); // I have received 5 parameters.
〆凄凉。 2024-10-20 01:18:35

您可以使用反射:

$refclass = new ReflectionClass($controller);
$param_count = $refclass->getMethod($method)->getNumberOfRequiredParameters();
if ($param_count <= count($this->params)) {
    $refclass->invokeArgs($controller, $this->params);
} else {
    // 404
}

可以通过检查参数是否过多来变得更严格。

You could use reflection:

$refclass = new ReflectionClass($controller);
$param_count = $refclass->getMethod($method)->getNumberOfRequiredParameters();
if ($param_count <= count($this->params)) {
    $refclass->invokeArgs($controller, $this->params);
} else {
    // 404
}

Can be made stricter by checking if there are too many parameters.

一百个冬季 2024-10-20 01:18:32

据我所知,如果不使用 reflection 就无法获取方法的参数计数。然而,它非常简单,看起来像这样:

$reflector = new ReflectionClass('Profiles');
$method = $reflector->getMethod('index');
$parameters = $method->getParameters();

printf('%s expects %d parameters', $method->name, count($parameters));

Will Yield

index expects 2 parameter

请注意,反射总是伴随着成本。然而,这对性能的影响应该不明显。

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:

$reflector = new ReflectionClass('Profiles');
$method = $reflector->getMethod('index');
$parameters = $method->getParameters();

printf('%s expects %d parameters', $method->name, count($parameters));

Will yield

index expects 2 parameter

Note that reflection always comes with a cost. However, the performance implications of this should not be noticable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文