title: Is it safe to set recursive properties with PHP object?
body: <p>I have looked everywhere and can't seem to find an answer. What I'm wondering is if it's safe or good practice to use recursive objects in PHP.  To create an object as a property of another object, that contains a property with a reference to the containing object...</p>
  
  <pre><code>&lt;?php
  
      class User
      {
          public $database;
  
          public function __construct() {
              $this-&gt;database = new Database($this);
          }
      }
  
      Class Database
      {
          private $user;
  
          public function __construct(User $user) {
              $this-&gt;user = $user;
          }
  
          public function doSomethingForUser() {
              // Call db or get info for this user
          }
      }
  </code></pre>
  
  <p>And then use it as follows...</p>
  
  <pre><code>    &lt;?php
          $bar = new User();
          $bar-&gt;database-&gt;doSomethingForUser();
      ?&gt;
  </code></pre>
  
  <p>I'be just noticed that i've had problems using certain functions such as array_multisort (Specifically "PHP Fatal error:  Nesting level too deep - recursive dependency?") when trying to sort an array of objects, and when I use var_dump, it shows some properties as being recursive...</p>
  
  <p>Any help is appreciated</p>