2012-07-16 89 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

相关问题