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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?