包含一次ob_get_contents未定义的变量

时间:2014-08-23 18:44:11

标签: php include ob-get-contents

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

admin.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 "";
      }
    }
}

可以接受吗?

0 个答案:

没有答案
相关问题