What is the best practice for accessing the configuration 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'); ?> 

Which of these options is the best solution? If not, indicate your option.

+4
source share
2 answers

I would go for option 1 (dependency injection).

Option 2 uses static methods, which, as already mentioned, simply prescribe global methods. And we all know that this is bad? is not it?

Option 3 is not my favorite because it is not enough OOP ;-), but serious: what if you (or the person using your code) want to change the format of the configuration file?

Option 4: global ...

Thus, basically my problems are with other options: testability, hard link, global.

So, option 1 is. I would also create an interface for the config class so that you can add another class for your configuration files at a later time. For instance. you (or someone else for that matter) want to use the XML files for configuration.

Another thing that I would change is private stuff. If someone wants to extend the class, he will not have access to variables in this way. My rule of thumb (not sure if everyone agrees with this, though) only makes the material private if you want people to have access to it (for example, it will change at some point). The danger of using private is that people will bypass it with hacks to do what they want.

Learn more about SOLID and see for more information.

Only my 2 cents.

+2
source

I would say that the first case is better if you replace $ config with $ onlyTheStuffThatMattersToThatFunction

+2
source

All Articles