Monday 15 March 2010

Associate array dependency for dependency injection in PHP OOP? -



Associate array dependency for dependency injection in PHP OOP? -

how doing deal class lots of dependency injections?

can store dependencies in an associate array , pass them in one?

class auth { // set props. protected $deps; protected $database; protected $constant; protected $passwordhash; // , on... /** * build data. */ public function __construct($deps) { // set di. $this->deps = $deps; $this->database = $this->deps['database']; $this->passwordhash = $this->deps['passwordhash']; // , on... } }

in container,

// set deps. $deps = [ "database" => $database, "passwordhash" => new passwordhash(hash_cost_log2, hash_portable), // , on... ]; // instantiate auth. // provide deps. $auth = new auth($deps);

i haven't done test unit test before. associate array dependency acceptable in design patter of di. can tested?

or improve solutions have got?

injecting array feels wrong. using constructor injection supposed forcefulness client inject dependencies. injecting array contains dependencies not forcefulness client @ all.

if of methods in auth class depends on objects have passed constructor in array, sense free , define dependencies separate parameters in constructor. if not, consider changing design. may have combined many functionality single class. if ok design of class , lets 2 methods depend on database, other 3 depend on passwordhash, 1 depends on session, etc., utilize setter injection non-common dependencies , inject them need them.

php arrays oop multidimensional-array dependency-injection

No comments:

Post a Comment