php - Managing global dependencies -


i writing large application , have started different design patterns write 'better code'. have started phpunit , have discovered code not testable without dependency injection, not able inject mock classes without it. i'm fine idea of dependency injection.

what having issues understanding more management of dependencies in project. specifically, have made global, such database connection. now, instead of being global variable, must inject dependency - that's fine.

however if have large number of function calls between instantiation of database object, , required, starts bit confusing me. doesn't make sense me each function have dependency upon database object. nor make sense me put in dependency container global dependencies , have every function in code have dependency on container.

imagine number of nested function calls, follows.

<?php      function foo()     {         bar();     }      function bar()     {         // need inject instance of database, don't have it!         baz();     }      function baz(database $database)     {         // work database here     }      $database = new database();     foo(); 

i can initialise database, may put in container. database class created once, , global application. code above, if baz database operation, have pass database instance both foo , bar. if put database in container, pass container instead. foo and, perhaps, bar need not care baz working database. , due nature of database class, poor design instantiate database every time need use it.

i have thought how combat issue, 1 of ideas use static ::getinstance method, more read on di, less method, feel wrong , i'm going wrong way.

another option can see have container class have purely static get/set methods, , static private array contain data. achieve there encapsulating global scope.

how should go managing global dependencies, , passing through chain instantiation injection?

you many opinions users here ;) use following approach in our company.

we have global factory class. class responsible managing dependencies. is, obviously, big because handles instantiation of possible objects testable. thing is, of methods in factory private. since need top-most objects. else hidden.

here example database. want collect products db.

class productcollector {     private $repository;      public function __construct(productrepository $repository)     {         $this->repository = $repository;     }      public function collect()     {         $collection = $this->repository->getallproducts();         // collection         // ...         return $collection;     } }  class productrepository {     private $pdo;      public function __construct(\pdo $pdo)     {         $this->pdo = $pdo;     }      public function getallproducts()     {        //... here can use pdo     } }  class factory {     public function createproductcollector()     {         return new productcollector($this->createproductrepository());     }      private function createproductrepository()     {         return new productrepository($this-createdbconnection());     }      private function createdbconnection()     {         // sake of example don't care how db credentials factory.         return new \pdo('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');     } } 

now in code can this:

$factory = new factory();  $collector = $factory->createproductcollector(); $collection = $collector->collect(); 

this is, of course, 1 of many possible solution deal di.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -