2012-07-16 63 views
4
<?php 
define('ABSPATH', dirname(__FILE__)); //Absolute path to index 

/* 
* Method 1 
* Dependency Injection 
*/ 
class Config{ 

    private $_config = NULL; 
    private $_filepath = NULL; 

    public function __construct($filepath){ 
     $this->_filepath = $filepath; 
     $this->load(); 
    } 

    private function load(){ 
     if ($this->_config === NULL){ 
      if (!file_exists($this->_filepath)){ 
       throw new Exception('Configuration file not found'); 
      }else{ 
       $this->_config = parse_ini_file($this->_filepath); 
      } 
     } 
    } 

    public function get($key){ 
     if ($this->_config === NULL){ 
      throw new Exception('Configuration file is not loaded'); 
     } 
     if (isset($this->_config[$key])){ 
      return $this->_config[$key]; 
     }else{ 
      throw new Exception('Variable ' . $key . ' does not exist in configuration file'); 
     } 
    } 
} 

function getLost($where, $why, $who){ 
    //do smth 
} 

try{ 
    $config = new Config(ABSPATH . '/app/config.ini'); 
    getLost('here', 'because', $config->get('who'));  
}catch(Exception $e){ 
    echo $e->getMessage(); 
} 
?> 

<?php 
/* 
* Method 2 
* Config is accessed via static class 
*/ 

class Config{ 

    private static $_config = NULL; 
    private static $_filepath = NULL; 

    public static function load($filepath){ 
     if (self::$_config === NULL){ 
      self::$_filepath = $filepath; 
      if (!file_exists(self::$_filepath)){ 
       throw new Exception('Configuration file not found'); 
      }else{ 
       self::$_config = parse_ini_file(self::$_filepath); 
      } 
     } 
    } 

    public static function get($key){ 
     if (self::$_config !== NULL){ 
      throw new Exception('Configuration file is not loaded'); 
     } 
     if (isset(self::$_config[$key])){ 
      return self::$_config[$key]; 
     }else{ 
      throw new Exception('Variable ' . $key . ' does not exist in configuration file'); 
     } 
    } 
} 

function getLost($where, $why){ 
    $who = Config::get('who'); 
} 

try{ 
    Config::load(ABSPATH . '/app/config.ini'); 
    getLost('here', 'because');  
}catch(Exception $e){ 
    echo $e->getMessage(); 
} 
?> 

<?php 
/** 
* Method 3 
* Config variable needed is passed as function parameter 
*/ 
$config = parse_ini_file(ABSPATH . '/app/config.ini'); 

function getLost($where, $why, $who){ 
    //do smth 
} 

getLost('here', 'because', $config['who']); 
?> 

<?php 
/* 
* Mathod 4 
* Config is accessed inside a function via global 
*/ 
$config = parse_ini_file(ABSPATH . '/app/config.ini'); 

function getLost($where, $why){ 
    global $config; 
    $who = $config['who']; 
} 

getLost('here', 'because'); 
?> 

以下哪個變種是最佳實踐的解決方案?如果沒有,請提供您的變體。訪問函數內部配置的最佳做法是什麼?

回答

2

$配置我會去的變體1(依賴注入)第一種情況比較好。

變體2使用static方法,這些方法如已經說明的那樣僅僅是global方法的另一種方式。我們都知道這是不對的?對?

變體3並不是我最喜歡的,因爲它不是OOP足夠;-)但是很嚴重:如果您(或使用您的代碼的人)想要更改配置文件的格式,該怎麼辦?

變4:global ...

所以基本上我與其他選項的問題是:可測性,緊耦合,全球性的。

所以變種1。我還會爲配置類創建一個接口,以便稍後您可以爲您的配置添加一個不同的類。例如。你(或者其他人)想要使用XML文件進行配置。

我會改變的另一件事是private東西。如果有人想擴大班級,他/她不會以這種方式訪問​​變量。我的經驗法則(不確定是否所有人都同意這一點)只會讓人們有機會訪問它(例如,它會在某個時刻發生變化)private。使用private的危險在於人們會通過黑客來繞過它來做他們想做的事。

瞭解更多關於SOLID並查看更多信息的谷歌clean code會談。

只是我2美分。

+2

我不一定會用毯子聲明,應該立即廢除同意與'私人'。並非所有算法都應該自動受到子類的修改。組成通常比繼承更可取。明智的「受保護」使用可以使黑箱保持「黑色」。這就是說,+1 :) – rdlowrey 2012-07-17 17:42:42

2

我說,如果你更換$ onlyTheStuffThatMattersToThatFunction

+1

不回答這個問題,但我必須+1,因爲你是對的,這是在這一點上唯一明智的答案:) – rdlowrey 2012-07-17 05:47:39

相關問題