jeudi 26 mars 2015

Hidden dependencies - why not?


Hidden dependencies:



function __construct($dep_registry){
$this->db = $dep_registry->get('db');
$this->request = $dep_registry->get('request');
...
}


Not so hidden:



function __construct(Db $db, Request $request, ....){
$this->db = $db;
$this->request= $request;
...
}


The disadvantages of the last is that, if I need to change the class later and need another dependency, I have to change the constructor arguments. And if I change the constructor, I need to also change every file that uses it. With hidden deps the change would only be needed in the two places: the constructor and the registry class. Another fail of the 2nd method is that it's not possible to lazy load dependencies, i always have to pass the instances


So what's the disadvantage of hidden deps and why is it bigger than this disadvantage of not so hidden deps?





Aucun commentaire:

Enregistrer un commentaire