It's about my HMVC project, in which I want to always pass configuration values as constructor dependencies in the classes that needs them.
Until now I could identify 4 categories of these configs:
- Structural: paths, namespaces, etc.
- Defaults: default file/dir names, default class suffixes, default app title, etc.
- Options: enable/disagle login, enable/disable logging, etc.
- Values: db connection parameters, HTTP status codes, etc.
Problem description:
I'm not sure, if this is a good idea as a whole: in the constructors, I wish to provide these configs as optional parameters.
If this is a good practice, then I also want to not hard-code their default values.
So, instead of:
abstract class AbstractView {
public function __construct($moduleName
, $modulesDirPath = '[...]/Modules' // Where all modules reside by default.
, $moduleNamePublics = 'Publics' // Default name of the dir where public resources reside (css, js, etc).
, $appLayoutsPath = '[...]/Layouts/Default' // Default name of the dir where the default layout for all module templates reside.
, $moduleTemplateName = 'Index' // The file name of the default template file ("Index.php").
, $appDomain = 'http://[...].com' // The web domain of the app using the framework.
) {
//...
}
}
I would think to have something like this:
use [...]\Constants;
abstract class AbstractView {
public function __construct($moduleName
, $modulesDirPath = Constants::MODULES_DIR_PATH
, $moduleNamePublics = Constants::MODULE_NAME_PUBLICS
, $appLayoutsPath = Constants::APP_LAYOUTS_PATH
, $moduleTemplateName = Constants::MODULE_TEMPLATE_NAME
, $appDomain = Constants::APP_DOMAIN
) {
//...
}
}
The reasons for doing this are:
1 - I don't want to make the config values as mandatory parameters in the constructors, because all of view classes inherited from AbstractView
would then have to be instantiated with all of these arguments.
2 - I want to keep the instances slim. Like:
class DashboardView extends AbstractView {
public function __construct($moduleName) {
parent::__construct($moduleName);
//...
}
}
My question is:
Even though my problem would be solved this way, I know that the Constants
class would introduce tight coupling on each class which use config values. So I'd like to ask you:
IS THERE ANOTHER BETTER (PROPER) WAY TO DO IT?
I'd like to mention, that I don't want to implement any singletons or static state in my app.
I'd appreciate any idea and help. Thank you very much.
Aucun commentaire:
Enregistrer un commentaire