2011-07-13 55 views
1

我需要在我的網站上有一些配置選項。在PHP項目中使用配置文件

我認爲如果在不同的文件中放置不同的選項,它會是最容易維護的。

另外我需要一個類來從不同的配置文件中檢索選項。

在我的網站的目錄結構,我創建了一個名爲/設置

在這個目錄中我有不同的配置選項,多個文件如:/setup/base.php

的內容base.php看起來像下面這樣:

$setup = new stdClass(); 
$setup->currencies = array('USD', 'EUR',); 
$setup->locations = array('local', 'international',); 

我想創建一個讀取文件並返回不同選項的類。

class Options 
{ 
    function __construct($option) 
    { 
     if (!is_file(SETUP_DIR.'/'.$option.'.php')) { 
      thrown new Exception('Configuration file not found.'); 
     } 

     $options = // get information from file 

     return $options; // this should return the currencies and locations 
    } 
} 

$options = new Options('base'); 

但是我不知道這是否是正確的做法。

如果是這樣,我不能想辦法從班級中的設置文件中檢索選項。

你能幫我解決這個問題嗎,或者至少讓我指向正確的方向?

+1

你有,你把你所有的加載庫註冊表? –

+0

只要在各種PHP文件中設置設置並將它們包括在內(在需要時使用'require_once()')會不會更容易? – Fabianius

+0

@Karl:nopez。雖然我可能只是去註冊表。 – PeeHaa

回答

2

好了,我不認爲這是一個正確的方式這一個:Zend的使用.ini文件,笨都有一套數組,而Symfony使用YAML。 Wordpress將數據庫中的大部分內容都存儲起來,並且只包含一個配置文件。我個人偏向ini文件 - ini是遍佈各處的東西,所以它有一種感覺,「我可以在必要時重用它」,但我認爲唯一的「錯誤的「這裏的解決方案是不一致的 - 如果你使用ini,使用ini,如果數組,數組,但不混合。

就你而言,有幾個選項。這兩個似乎是最常見的。(這兩個例子假定stdClass的對象是在加載文件名爲$options),你可以創建一個包裝:

class Options 
{ 
    private $_options; 
    function __construct($option) 
    { 
     if (!is_file(SETUP_DIR.'/'.$option.'.php')) { 
      thrown new Exception('Configuration file not found.'); 
     } 

     require(SETUP_DIR.'/'.$option.'.php'); 
     $this->_options = $options; 
     // you shouldn't put a return in a constructor 
    } 

    // this will point to the internal _options variable. 
    // making it a read-only access to the values from $option 
    public function __get($name){ return $this->_options->$name; } 
} 

或者,你可以使用Singleton模式,只是在個別類返回的對象:

class OptionsRetriever 
{ 
    private $_fetched; 
    private static $_instance; 

    private __construct(){} 

    public static function &getInstance() 
    { 
     if(!isset(self::$_instance)) self::$_instance = new OptionsRetriever(); 
     return self::$_instance; 
    } 

    public function getConfig($name) 
    { 
     if(!isset($this->_fetched[ $name ])) 
     { 
      require(SETUP_DIR.'/'.$name.'.php'); 
      $this->_fetched[ $name ] = $options; 
     } 
     return $this->_fetched[ $name ]; 
    } 
} 

或者,你可以將它們結合起來:

class Options 
{ 
    private $_options; 
    function __construct($options) 
    { 
     $this->_options = $options; 
    } 

    public function __get($name){ return $this->_options->$name; } 
} 

    // replace getConfig with this 
    public function getConfig($name) 
    { 
     if(!isset($this->_fetched[ $name ])) 
     { 
      require(SETUP_DIR.'/'.$name.'.php'); 
      $this->_fetched[ $name ] = new Options($options); 
     } 
     return $this->_fetched[ $name ]; 
    } 
1

您可以在PHP中使用ini文件的概念和parse_ini_file函數來完成此操作。在php.net功能頁面上有清晰的例子:parse_ini_file @ PHP.NET

0

如果你不想直接存儲在PHP中的值,你需要包含PHP文件中的值,例如變量,常量或類。

我喜歡使用的方法是將配置存儲在XML文件中,然後加載並解析它。如果您更喜歡這種格式,則可以使用與JSON相同的方法。只要確保你阻止任何瀏覽器訪問這些文件。

1

你可以只包括base.php文件,像這樣:

class Options 
{ 
    function __construct($option) 
    { 
     if (!is_file(SETUP_DIR.'/'.$option.'.php')) { 
      thrown new Exception('Configuration file not found.'); 
     } 

     include_once(SETUP_DIR.'/'.$option.'.php'); 

     $options = $setup; // make sure your variable in the config is allways named $setup 

     return $options; // this should return the currencies and locations 
    } 
} 

$options = new Options('base'); 
echo $options->currencies[0]; //should print 'USD' provided that your base.php file from the question was used.