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}();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!