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

时间:2013-11-07 02:54:10

标签: php smarty

我正在创建一个显示页面的控制器。我现在有这个;

 $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);

在setup.php页面上,然后在我的libs上:

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/');

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

$this->$PageName;

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

1 个答案:

答案 0 :(得分:3)

看看call_user_func()call_user_func_array()函数,他们可以更有意义的方式完成此任务:

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

顺便说一句,这可以通过变量变量来实现:

$this->{$PageName}();