title: Passing an entire Class as a parameter within another Class
body: <p>So far I feel like I've understood the concept and the advantages of OOP programming, and I've not really had any difficulties with understanding how to work with classes in PHP.</p>
  
  <p>However, this has left me just a little confused. I think I may understand it, but I'm still uncertain.</p>
  
  <p>I've been following a set of video tutorials (not sure on the rules on linking to outside resources, but I found them on youtube), and they're pretty self explanatory. Except, frustratingly, when the tutor decided to pass one class as a parameter within another class. At least I think that's what's happening;</p>
  
  <pre><code>Class Game
  {
  
      public function __construct()
      {
          echo 'Game Started.&lt;br /&gt;';
      }
      public function createPlayer($name)
      {
          $this-&gt;player= New Player($this, $name);
      }
  }
  
  
  Class Player
  {
  
      private $_name;
  
      public function __construct(Game $g, $name)
      {
          $this-&gt;_name = $name;
          echo "Player {$this-&gt;_name} was created.&lt;br /&gt;";
      }
  }
  </code></pre>
  
  <p>Then I'm instantiating an object of the Game class and calling its method;</p>
  
  <pre><code>$game = new Game();
  $game-&gt; createPlayer('new player');
  </code></pre>
  
  <p>Rather frustratingly, the tutor doesn't really explain why he has done this, and hasn't displayed, as far as I can see, any calls in the code that would justify it.</p>
  
  <p>Is the magic method constructor in Player passing in the Game class as a reference? Does this mean that the entire class is accessible within the Player class by reference? When referencing $this without pointing to any particular method or property, are you referencing the entire class?</p>
  
  <p>If this is what is happening, then why would I want to do it? If I've created a Player inside my Game Class, then surely I can just access my Player Properties and Methods within the Game Class, right? Why would I want my Game Class inside my Player class as well? Could I then, for example, call createPlayer() within the Player class?</p>
  
  <p>I apologise if my explanation has been at all confusing.</p>
  
  <p>I guess my question boils down to; what is it that I'm passing as a parameter exactly, and why would I want to do it in every day OOP programming?</p>