duanou8504 2014-01-08 13:33
浏览 113
已采纳

PHP注意:包含另一个文件并在函数中从中获取变量时的未定义变量

I have the following two files, first is for config options, and the second contains some functions. When I try to get the variable from config.php in functions.php then I get error:

Notice: Undefined variable: config in /var/www/app/functions.php on line 15

Config in file config.php

$config = array('page_title' => 'Page Title');

File functions.php

require_once 'config.php';

function get_header() {
  $header = new Template( 'header' );
  $header->set( 'pagetitle', $config['page_title'] );
  echo $header->output();
}

When I tried to place the config variable inside the function it works correctly. Why I can do this my way?

  • 写回答

3条回答 默认 最新

  • douchenzhan3050 2014-01-08 13:35
    关注
    function get_header() {
    
      global $config;
    
      $header = new Template( 'header' );
      $header->set( 'pagetitle', $config['page_title'] );
      echo $header->output();
    }
    

    Basically, you're using global variable in a local context.

    It would be a good idea to encapsulate config in some kind of Config class, with singleton, so the config does not get overwritten by anything.

    To be totally compliant with almost good OOP practices ;)

    class Config {
    
     protected $data;
    
     public function __construct(array $config) {
    
      $this->data = $config;
    
     }
    
     public function get($key) {
    
      return $this->data['key'];
    
     }
    
    }
    
    
    class ConfigManager {
    
     public static $configs;
    
     // In "good OOP" this should't be static. ConfigManager instance should be created in some kind of initialisation (bootstrap) process, and passed on to the Controller of some sort
     public static function get($configName) {
    
      if(! isset(self::$configs[$configName]))
       self::$configs[$configName] = new Config(include('configs/' . $configName. '.php')); // in good OOP this should be moved to some ConfigReader service with checking for file existence etc
    
      return self::$configs[$configName];
    
     }
    
    }
    

    and then in configs/templates.php

    return array('page_title' => 'Page Title');
    

    your function would look like this:

    function get_header() {
    
      $config = ConfigManager::get('templates');
    
      $header = new Template( 'header' );
      $header->set( 'pagetitle', $config->get('page_title') );
      echo $header->output();
    }
    

    This may seem overly complicated, and of course you don't have to follow this kind of practices, but the more you code, the more you will enjoy good practices.

    Using globals is not one of them!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?