title: PHP json_encode class private members
body: <p>I'm trying to JSON encode some objects in PHP, but I'm facing a problem: I want to encode data which is kept by a class private members.
  I found this piece of code to encode this object by calling an encode function like:</p>
  
  <pre><code>public function encodeJSON() 
  { 
      foreach ($this as $key =&gt; $value) 
      { 
          $json-&gt;$key = $value; 
      } 
      return json_encode($json); 
  }
  </code></pre>
  
  <p>However, this only works if the object I want to encode does not contain other objects inside, which is the case. How can I do to encode not only the "outer" object, but encode as well any members that are objects too?</p>