这是在函数内访问配置的最佳实践


Which is the best practice to access config inside a function?

<?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');
?>

以下哪种变体是最佳实践解决方案?如果没有,请提供您的变体。

我会选择变体 1(依赖注入(。

变体 2 使用static方法,如前所述,这些方法只是另一种global方法。我们都知道这很糟糕,对吧?右?

变体 3 不是我最喜欢的,因为它不够 OOP ;-(,但很严重:如果您(或使用您的代码的人(想要更改配置文件的格式怎么办?

变体 4:global ...

所以基本上我对其他选项的问题是:可测试性、紧密耦合、全局。

所以变体 1 是。我还会为 config 类创建一个接口,以便您稍后可以为配置内容添加不同的类。例如,您(或其他人(想要使用XML文件进行配置。

我要改变的另一件事是private东西。如果有人想扩展类,他/她将无法以这种方式访问变量。我的经验法则(不确定是否每个人都同意这一点(是,只有在您希望人们访问它时才private制作东西(例如,它会在某个时候发生变化(。使用private的危险在于,人们会通过黑客绕过它来做他们想做的事。

阅读更多关于 SOLID 的信息,并查看 Google 清洁代码讨论以获取更多信息。

只有我的2美分。

我会说如果你用$onlyTheStuffThatMattersToThatFunction替换$config,第一种情况会更好