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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭