2013-12-15 68 views
0

我有了這個SOURCE_FOLDER/config.php文件文件:包含的文件包含它包含的文件嗎?

<?php 


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

?> 

SOURCE_FOLDER /班/ 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 /班/config.class.php file:

<?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?從我知道它應該是,但它似乎沒有。
編輯:我也試圖把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變量本身不是。爲什麼?

+0

上http://www.php.net/manual/en/language.variables.scope.php – mario

+0

這是有關的變量可用性閱讀起來函數,我知道我不能返回'$ config [$ name]',因爲變量是在類和方法的作用域之外聲明的。但是爲什麼當我試圖在同一個文件中轉儲類的變量時,它會轉儲爲* NULL *? –

+0

實際全局作用域中$ config數組的存在取決於調用include腳本的位置。包含的腳本不在默認情況下在全局範圍內運行。自動裝載機具有本地功能範圍。 – mario

回答

0

將配置作爲返回變量的函數本身

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。如果我只是在我的瀏覽器中加載config.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) 
+0

我不希望** config.php **文件中有任何函數,但我解決了它,謝謝。無論如何,我的問題是別的,你能查看我最後的編輯嗎? –

+0

我用我的結果更新了我的帖子。只要它不在函數或類中,就可以在任何包含它的文件中看到它。 – mseifert

+0

我得到了不同的結果,也許這是我的** php.ini **設置的問題。無論如何,感謝您的幫助,我越來越好,每天都在這個:) –