使用 $this->$Var 调用现有方法


using $this->$Var to call an existing method

我正在创建一个控制器来显示页面。我目前有这个;

 $request  = str_replace("/Smarty/", "", $_SERVER['REQUEST_URI']); 
   $params = explode("/", $request);  
  function FormatArr ($Arr){
        $Array_Keys = array ("PageName","Username");
        if (count($Arr) > 2){
            trigger_error("Unexpected Params",E_USER_ERROR);
        }
        return array_combine($Array_Keys,$Arr);
    }
    $New_Params = FormatArr($params);

在设置.php页面上,然后在我的库上:

class testing {
        protected $Smarty;
        protected $fixpath;
        public function __construct($Template_Name){
            $this->Smarty = new Smarty;
            $this->fixpath = dirname(__FILE__)."./Templates/".$Template_Name;           
            $this->Smarty->compile_dir=$this->fixpath."/compile";
            $this->Smarty->template_dir=$this->fixpath."/html";
        }
        public function index(){
            $this->Smarty->assign("name","test");
            $this->Smarty->assign("test","../test/");   
        }
        public function Display_Page($PageName){
            $this->$PageName();
            $this->Smarty->display($PageName.".html");
        }
    }
    $Test = new testing('Testing/');

我让它成功工作,但我想动态调用将在智能模板上呈现正确变量的页面。该问题是由以下原因引起的:

$this->$PageName;

我正在努力寻找使这种成功调用必要方法的方法

看看 call_user_func() 和 call_user_func_array() 函数,它们可以通过更有意义的方式完成此操作:

call_user_func(array($this, $PageName));

顺便说一下,这将使用变量变量来解决问题:

$this->{$PageName}();