title: PHP OOP proper class communication
body: <p>Is there a <strong><em>good practice</em></strong> way of making communication between classes?</p>
  
  <p>For example, I have a CMS I'm working on. In <strong>main</strong> class it instantiates two new Classes <strong><em>ThemeManager</em></strong> and <strong><em>ModuleManager</em></strong>. Now, I need my <em>Theme Manager</em> to be able to access <em>Module Manager</em> so that it can read loaded modules and print their content in a page.</p>
  
  <p>Is this <em>OK</em> ? Making these two classes public properties of <strong>main</strong> class. This enables me two-way communication (which I don't necessarily need in this situation):</p>
  
  <pre><code>class MainClass {
      public $ThemeManager;
      public $ModuleManager;
  
      ..........
  
      function instantiate_managers() {
           $this-&gt;ThemeManager = new ThemeManager($this);
           $this-&gt;ModuleManager = new ModuleManager($this);
      }
  }
  </code></pre>
  
  <p>Or is it this better? Passing one already instantiated to another, since one-way communication will do <em>OK</em> for my purpose:</p>
  
  <pre><code>class MainClass {
      private $ThemeManager;
      private $ModuleManager;
  
      ..........
  
      function instantiate_managers() {
           $this-&gt;ModuleManager = new ModuleManager();
           $this-&gt;ThemeManager = new ThemeManager($this-&gt;ModuleManager);
      }
  }
  </code></pre>
  
  <p>Note that in either way I have to pass these references further to <em>Module</em> class and <em>Theme</em> -> <em>Layout</em> classes.</p>
  
  <p>Maybe it would be better to declare <strong>ThemeManager</strong> and <strong>ModuleManager</strong> in global scope since they will be instantiated only once anyway and saves me the trouble of passing their references through each <strong>__<em>construct</em></strong> ? I've read somewhere that this is bad practice in OOP and I'm trying to avoid it, but in situations like these this seems like the <em>least</em> hacky way to do it.</p>
  
  <p>Is there a 4th good way to handle communication which I haven't tought about?</p>
  
  <h1>Answer</h1>
  
  <p>I ended up using <a href="http://pimple.sensiolabs.org/" rel="nofollow">Pimple</a> for <strong>dependency injection</strong> method since it felt most <em>natural</em> and was suggested in both answers. </p>