title: Nested Objects in PHP
body: <p>Likely this has already been asked, but nevertheless, here goes. This may fall under best practice or security... I'm not really sure.</p>
  
  <p>In my application, I am using a nested object, that is called in the <code>__construct()</code> function. Sort of like this:</p>
  
  <pre><code>class user {
      public $userID = NULL;
      public $someObject = NULL;
  
      public function __construct() {
          $this-&gt;userID = getThisUser();
          $this-&gt;someObject = new objectBuilder($this-&gt;userID);
      }
      public function getThisUser() {
          // ... 
      }
  }
  
  class objectBuilder {
      public $buriedVar = NULL;
  
      public function __construct($uid = NULL) {
          if( !isset($uid) ) {
              $this-&gt;buriedVar = setTheObject($uid);
          } else {
              $this-&gt;buriedVar = setTheObject(0);
          }
      }
      public function setTheObject($id) {
          // ...
          return "random string";
      }
  }
  
  $tom = new user();
  </code></pre>
  
  <p>Obviously terrible outline here, but the point is, I can then call <code>$tom-&gt;someObject-&gt;buriedVar</code> and it'll return <code>"random string"</code>.</p>
  
  <p>While looking for a way to nest classes, I noticed no one recommends this as a method for storing objects inside of another object. I'm curious of a few things:</p>
  
  <p>1) Is this insecure?<br>
  2) Are the vars inside the nested object exclusive to the call made inside <code>$tom-&gt;__construct()</code>, or if I create another object using <code>new objectBuilder()</code> is it overwriting the one inside <code>$tom-&gt;someObject</code>? I haven't noticed this, but am not sure how to test for that entirely.<br>
  3) Is there something else I'm missing? A best practice reason not to instantiate an object inside a class? I've been using it for years and it works great for what I've done. Is it a speed thing?</p>