title: Circular dependency - Injecting objects that are directly depended on each other
body: <p>I have used <a href="https://github.com/TomBZombie/Dice" rel="noreferrer"><em>Dice PHP DI container</em></a> for quite a while and it seems the best in terms of simplicity of injecting dependencies.</p>
  <p>From <a href="http://r.je/dice.html" rel="noreferrer"><em>Dice Documentation</em></a>:</p>
  <pre><code>class A {
      public $b;
      
      public function __construct(B $b) {
          $this-&gt;b = $b;
      }
  }
  
  class B {
      
  }
  
  $dice = new \Dice\Dice;    
  $a = $dice-&gt;create('A');
  var_dump($a-&gt;b); //B object
  </code></pre>
  <p>However, when you have to use objects that are directly dependent on each other, the finall result is server error, because of the <em>infinite loop</em>.</p>
  <p>Example:</p>
  <pre><code>class A {
      public $b;
  
      public function __construct(B $b) {
          $this-&gt;b = $b;
      }
  }
  
  class B {
      public $a;
  
      public function __construct(A $a) {
          $this-&gt;a = $a;
      }
  }
  </code></pre>
  <p>Author of <em>Dice</em> says that there is no way to construct an object from the A or B classes. As:</p>
  <ul>
  <li>An 'A' object requires a 'B' object to exist before it can be created</li>
  <li>But a 'B' object requires an 'A' object to exist before it can be created</li>
  </ul>
  <p>Author says, that this limitation concerns <strong>all</strong> <em>DI containers</em>!</p>
  <hr />
  <h3>Question:</h3>
  <p>What would be the best solution for overcoming this problem <em><strong>nicely</strong></em> without changing initial code? Could anyone provide an example of using other <em>DI containers</em>, when it would be possible to run <em>exampled code</em> <em><strong>without</strong></em> bulky workarounds?</p>