I am looking for the most efficient way to apply a set of properties (configuration) to a newly created instance. My first goal is to support the object orientation of the application, the second is the ability to work with the DI container. This is the sample that I have come up with so far:
class ViewLogin { public $msgLoginGranted; public $msgLoginFailed; public function __construct(){ } protected function onSuccess() { return $this->msgLoginGranted; } protected function onFailure() { return $this->msgLoginFailed; } } class ControllerLogin { public function __construct(ModelLogin $model, ViewLogin $view) { } }
To keep ViewLogin clean and separate configuration data from the code, what is best to do:
Create a new ViewLogin1 class
class ViewLogin1 extends ViewLogin { public function __construct() { $this->msgLoginGranted = 'Welcome!'; $this->msgLoginFailed = 'Login Failed!'; } }
CONS: static class content, no new features, polluting class space
Pass the configuration object to ViewLogin
class ViewLogin { public function __construct(Config1 $config) { $this->msgLoginGranted = $config->msgLoginGranted; $this->msgLoginFailed = $config->msgLoginFailed; } }
Create a decorator for ViewLogin?
Move configuration to XML / JSON / YAML ...
source share