在PHP中将参数动态解析为方法

在PHP中将参数动态解析为方法,php,Php,我正在为自己创建一个快速的框架,一个东西已经基本就绪,但我这里有一些问题 解析URL时,这里是约定http://example.com/controller/method/(param)/(param)/(param)。我在url数组的长度上使用了switch语句 这是代码 switch (count($this->_url)) { case 5: //Controller->Method(Param1, Param2, Param3)

我正在为自己创建一个快速的框架,一个东西已经基本就绪,但我这里有一些问题

  • 解析URL时,这里是约定
    http://example.com/controller/method/(param)/(param)/(param)
    。我在
    url
    数组的长度上使用了
    switch
    语句
  • 这是代码

    switch (count($this->_url)) {
            case 5:
                //Controller->Method(Param1, Param2, Param3)
                $this->_controller->{$this->_url[1]}($this->_url[2], $this->_url[3], $this->_url[4]);
                break;
    
            case 4:
                //Controller->Method(Param1, Param2)
                $this->_controller->{$this->_url[1]}($this->_url[2], $this->_url[3]);
                break;
    
            case 3:
                //Controller->Method(Param1)
                $this->_controller->{$this->_url[1]}($this->_url[2]);
                break;
    
            case 2:
                //Controller->Method()
                $this->_controller->{$this->_url[1]}();
                break;
    
            default:
            $this->param = $this->_url[1] .','. $this->_url[2];
                $this->_controller->index();
                break;
        }
    
    这里是我的问题,如果我有一个论坛
    http://example.com/forum
    开关
    将运行默认值,即控制器
    索引()
    方法。由于开关块,我不能有这样的
    http://example.com/forum/$id
    因为索引不接受任何参数,实际上我无法为每个论坛id编写不同的方法。解决方法是在类中创建
    p
    方法,并在参数中进行适当的解析url现在如下
    http://example.com/forum/p/$id
    但我不太喜欢url中的p

    以下是我尝试的内容 在开关块上方,我检查了该方法(从url的1索引中获取)是否存在。如果没有,它应该获取额外的参数,并使它们成为index方法的参数

    $this->param = NULL;
        $ct = 1;
        for ($ct; $ct < count($this->_url); $ct++) {
            if(isset($this->_url[$ct]) && $this->_url[$ct] != null) {
                if (!method_exists($this->_controller, $this->_url[$ct])) {
                    $length = 0;
                    $this->param .= $this->_url[$ct].', ';
                }
            }
        }
    
    $this->param=NULL;
    $ct=1;
    对于($ct;$ct\uURL);$ct++){
    如果(isset($this->\uURL[$ct])&&$this->\uURL[$ct]!=null){
    如果(!method_存在($this->_controller,$this->_url[$ct])){
    $length=0;
    $this->param.=$this->_url[$ct]。,';
    }
    }
    }
    
    默认的切换条件现在是
    $this->\u controller->index($this->param)
    通过抓取参数,它可以正常工作,但它仅被视为一个参数,即,如果
    $this->param=“24,general”
    的结果不被视为
    索引($id,$section)
    方法中的
    $section
    而不是
    方法中的
    $section
    ,“常规”
    索引($id,$section)
    中被视为
    $id

    我想将每个获取的
    $this->param
    解析为
    index()
    方法中的一个参数


    谢谢,很抱歉提出了这么长的问题。

    我建议使用
    调用用户函数数组


    也许您需要根据您的特殊需要对其进行一些修改,但通常情况下,这将适用于您的情况。

    请参阅。应该很有趣。是的,这很有效,谢谢。但是将
    调用用户函数数组()
    与上面提到的反射方法进行比较,您认为哪种方法对应用程序有效?在我看来,在这种情况下,反射是无法克服的。反射主要用于“解析”代码,用于反向工程或类似的工作。请参见
    call\u user\u func\u array
    没有解析整个类本身的开销。
    call_user_func_array(array($this->_controller, $this->_url[1]), $this->_url);
    // this will pass all all params to the defined function