包含的文件是否包含包含的文件


does included file contain files included by it?

我有一个source_folder/config.php文件:

<?php

$config['database'] = array (
  'host' => 'localhost',
  'user' => 'root',
  'pass' => '',
  'db' => 'game'
);
?>

这个source_folder/class/core.class.php文件:

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'config.php';
function __autoload($sName) {
    $aName = explode('_',$sName);
    if($aName[0] == 'Model')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/model/' . strtolower($aName[1]) . '.class.php';
    elseif($aName[0] == 'View')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/view/' . strtolower($aName[1]) . '.class.php';
    elseif($aName[0] == 'Controller')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/controller/' . strtolower($aName[1]) . '.class.php';
    elseif($aName[0] == 'Core')
        include_once $_SERVER['DOCUMENT_ROOT'].'class/' . strtolower($aName[1]) . '.class.php';
}
class Core {
}

这个source_folder/class/config.class.php文件:

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'class/core.class.php';
/**
 * Description of config
 *
 * @author Lysy
 */
class Core_Config extends Core {
    static function GetConfigArray($name) {
        return $config[$name];
    }
}
?>

当我将var_dump($config['database']);放入core.class.php中时,结果是变量的转储。但当我把var_dump(Core_Config::GetConfigArray('database'));放在任何地方时,它都会转储为NULL。问题出在哪里?core.class.php中包含的config.php是否也包含在config.class.php,因为它包含了core.class.php?据我所知,它应该是,但似乎不是
EDIT:我还试图将var_dump($config['database']);放入config.class.php中,但它也会转储为NULL

编辑2:我使用解决了它

class Core {
    static public function getWholeConfig() {
        global $config;
        return $config;
    }
}

core.class.php文件和中

static function GetConfigArray($name) {
    $config = Core::getWholeConfig();
    return $config[$name];
}

config.class.php文件中,但我仍然不明白为什么最后一个文件没有看到$config变量。我的意思是,不在类范围内,但在任何地方,这个变量都包含在core.class.php中,尽管core.class.php包含在config.class.php,但变量本身不是。为什么?

将config作为返回变量放置到这样的函数中

function getConfig(){
   $config['database'] = array (
     'host' => 'localhost',
     'user' => 'root',
     'pass' => '',
     'db' => 'game'
   );
   return $config;
}

然后在你的课堂上你可以使用:

   $config = getConfig();
   return $config[$name];

我把var_dump($config);放在config.class和core.class的顶部

array (size=1)
  'database' => 
    array (size=4)
      'host' => string 'localhost' (length=9)
      'user' => string 'root' (length=4)
      'pass' => string '' (length=0)
      'db' => string 'game' (length=4)
array (size=1)
  'database' => 
    array (size=4)
      'host' => string 'localhost' (length=9)
      'user' => string 'root' (length=4)
      'pass' => string '' (length=0)
      'db' => string 'game' (length=4)