Re: When to instantiate a new class

From: Date: Sat, 27 Jun 2015 18:05:18 +0000
Subject: Re: When to instantiate a new class
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-324971@lists.php.net to get a copy of this message
On 06/20/2015 08:00 AM, Jesse Schalken wrote:
Also also, I've never quite figured out why "dependency injection containers" exist. Dependency injection just means you pass through to a class/method the things it needs, rather than letting it instantiate them itself, so the caller can decide how those dependencies are implemented. If lots of things need the same set of dependencies, then just wrap them all up in another class that has properties or getters for each. It's not hard.
What you describe is a service locator. Which is basically the same thing as a dependency injection container that you pass into the object you're using. Google "service locator bad" for why you shouldn't do that. :-) --Larry Garfield

« previous php.general (#324971) next »