In my MVC project (PHP 7.1) I have a Config
class and a multitude of config files, each of them looking like this:
return [
'web' => [
'host' => 'localhost',
//...
],
];
At the app entry point I make an instance of the Config
class and load all config file arrays in it. The created object is then passed as constructor argument in all classes that needs it:
class AbstractView {
private $config;
public function __construct(Config $config) {
$this->config = $config;
}
private function prepareContext() {
$this->assign('appHost', $this->getConfig()->get('web/host'));
}
}
Because it's quite a "big effort" to pass the Config object quite overall in my app, I thought of implementing a
Config` class with only class constants in it, like:
class Config {
const WEB_PROTOCOL = 'http';
const WEB_HOST = 'localhost';
}
and access them from overall directly with
class AbstractView {
private function prepareContext() {
$this->assign('appHost', Config::WEB_HOST);
}
}
I wanted to ask: Is this a really good alternative, having in mind that I want to completely avoid static states in my app? Is this alternative a "static" one or absolute not? What about testability (I don't have any experience with it yet)?
Thank you very much.
Aucun commentaire:
Enregistrer un commentaire