Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency-injection in real life
    primarykey
    data
    text
    <p>I am building a really minimal MVC framework to increase my PHP knowledge and challenge myself. I've come to the point where Classes begin to be dependent on each other to work. <a href="http://fabien.potencier.org/article/11/what-is-dependency-injection" rel="nofollow noreferrer">Dependency injection</a> seems to be the solution to this and are used by some of the <a href="http://components.symfony-project.org/dependency-injection/" rel="nofollow noreferrer">big frameworks</a> around. </p> <p>I've found <a href="https://github.com/troelskn/bucket" rel="nofollow noreferrer">Bucket</a> on Github and have messed around it for a while to understand the basics. <strong>What I can't get my head around however is when it's appropriate to create a Container?</strong></p> <p>Making one big container including every possible class that <em>may be needed</em> seems nothing but counter-productive to me and I can't imagine that it is good practice. It seems like the recipe for bad performance at least.</p> <p>In the alternative, which is to make multiple containers, I still don't get how the ever-stinking Singletons are not needed anymore.</p> <p>Let's say that I would have the following code:</p> <pre><code>$session_container = new bucket_Container(); $session_container-&gt;create('Database'); $session_container-&gt;create('Database_Sessions'); $log_container = new bucket_Container(); $log_container-&gt;create('Database'); $log_container-&gt;create('Database_Log'); </code></pre> <p>So here we have two containers, or in this case <em>buckets</em> for two totally different usages which are mutual by their dependency on the <code>Database</code> class.</p> <p>My logic tells me that the above code will create two independent instances of the <code>Database</code>-class, meaning that I still would have to make the <code>Database</code>-class a singleton to ensure that concurrent instances of my database connection isn't occurring?</p> <p><strong>Is this correct?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload