Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 包含once ob_get_内容未定义的变量_Php_Include_Ob Get Contents - Fatal编程技术网

Php 包含once ob_get_内容未定义的变量

Php 包含once ob_get_内容未定义的变量,php,include,ob-get-contents,Php,Include,Ob Get Contents,在我的admin.php中,我设置了$controller,它作为一个动态CMS工作得很好,问题是我使用的模板文件无法识别其中的$controller Admin.php Cms.php 任何模板页面 哪一个回显注意:未定义变量:中的控制器 如果我不必每次都调用$controller,就可以访问$controller,这会更容易。我是否遗漏了什么?为什么$controller在任何模板页面上都没有定义 更新 对我来说似乎有效的方法是像这样再次添加var Class CMS{ /*New t

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

Admin.php

Cms.php

任何模板页面

哪一个回显注意:未定义变量:中的控制器

如果我不必每次都调用$controller,就可以访问$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 "";
      }
    }
}

这可以接受吗?

所以基本上我应该做一些类似于函数实例化{include_once'configuration.php';require'cms.php';return global$controller=new cms;}的事情,因为我认为当你包含一个文件时,它基本上就像该文件已经存在一样,并且可以在不必重新定义它们的情况下传入变量。
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);
Class CMS{
   /*New template function*/
   function template($file){
      if( isset($file) && file_exists($file.".php") ){
         $controller = $this;
         include_once($file.".php");
      }else{
         echo "";
      }
    }
}