0

在我的 admin.php 中,我设置了我的$controller,它作为动态 CMS 工作得很好,我遇到的问题是我用作模板的文件$controller在其中无法识别。

管理员.php

include_once("configuration.php");
require("cms.php");
$controller = new CMS();
...
$controller->template("body");

CMS.php

Class CMS{
   /*New template function*/
   function template($file){
      if( isset($file) && file_exists($file.".php") ){
         include_once($file.".php");
      }else{
         echo "";
      }
    }

  /*Old template function*/
  function template($file){
     if(isset($file) && file_exists($file.".php")){
        ob_start();
        include($file.".php");
        $template = ob_get_contents();
        return $template;
       } else {
         echo "";
       }
   }

}

任何模板页面

var_dump($controller);

呼应Notice: Undefined variable: controller in

$controller如果我无需每次都调用它就可以访问它会更容易。我在这里遗漏了什么,为什么$controller在任何模板页面上都没有定义?

更新

似乎对我有用的是像这样再次添加var

Class CMS{
   /*New template function*/
   function template($file){
      if( isset($file) && file_exists($file.".php") ){
         $controller = $this;
         include_once($file.".php");
      }else{
         echo "";
      }
    }
}

这是可以接受的吗?

4

0 回答 0