Re: When to instantiate a new class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 27 June 2015 19:05:18 BST, Larry Garfield <larry@xxxxxxxxxxxxxxxx> wrote:
>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

I do think in some circumstances it's the responsibility of the class itself to ensure only one instance of itself exists.

Consider a class that loads in file excerpts on a loop, and stores them in the object structure to avoid repetitive disk reads that aren't necessary. If the class is set up as a Singleton, it can't be misused by other code. Dependency injection won't solve that. 

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux