mercredi 7 juin 2017

PHP MVC How to better define default values for the class constructor and method parameters?

It's about my HMVC project, which should not contain any static class members, static methods, or singletons. I'm using a dependency injection container. The M, V & C components are encapsulated in "independent" blocks, e.g. in modules (module directories).

I need to decide the best way of assigning default values to the parameters of the class constructors and methods.


Option 1: "Hard-code" the default values:

abstract class AbstractView {

    public function __construct(
        , $appLayoutsPath = '[app-root-path]/Layouts/Default'
        , $moduleLayoutsPath = '[module-root-path]/Templates/Layouts'
        , $moduleTemplatesPath = '[module-root-path]/Templates/Templates'
    ) {
        //...
    }

}


Option 2: Assign the default values by reading them from a class containing only class constants:

class Constants {

    const APP_LAYOUTS_PATH = '[app-root-path]/Layouts/Default';
    const MODULE_LAYOUTS_PATH = '[module-root-path]/Templates/Layouts';
    const MODULE_TEMPLATES_PATH = '[module-root-path]/Templates/Templates';

}

abstract class AbstractView {

    public function __construct(
        , $appLayoutsPath = Constants::APP_LAYOUTS_PATH
        , $moduleLayoutsPath = Constants::MODULE_LAYOUTS_PATH
        , $moduleTemplatesPath = Constants::MODULE_TEMPLATES_PATH
    ) {
        //...
    }

}


I'd like to ask you:

  1. From your perspective, which option would be the proper one? Can you please tell me why?
  2. Regarding the second option: Is class Constants introducing tight coupling? Or maybe low cohesion? Or maybe some other form of interdependence?
  3. Is there a better alternative?

Any idea or small tip based on your experience will be a big help for me.
Thank you very much.

Aucun commentaire:

Enregistrer un commentaire