dougou2937 2013-11-07 02:54
浏览 44
已采纳

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

I'm in the process of creating a controller to display pages. I currently have this;

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

On the setup.php page, then on my 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/');

I have it sucessfully working, but I want to dynamically call pages which will render the correct variables on the smarty template. The problem is caused by:

$this->$PageName;

I'm struggling on finding the way of making this sucessfully call the necessary method

  • 写回答

1条回答 默认 最新

  • dongshai2022 2013-11-07 02:55
    关注

    Take a look at call_user_func() and call_user_func_array() functions, they can accomplish this in a more meaningful way:

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

    By the way, this would do the trick with variable-variable:

    $this->{$PageName}();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch