title: json_encode on class with magic properties
body: <p>I am trying to <code>json_encode</code> an array of objects who all have <strong>magic properties</strong> using <code>__get</code> and <code>__set</code>. <code>json_encode</code> completely ignores these, resulting in an array of empty objects (all the normal properties are <code>private</code> or <code>protected</code>).</p>
  
  <p>So, imagine this class:</p>
  
  <pre><code>class Foo
  {
      public function __get($sProperty)
      {
          if ($sProperty == 'foo')
          {
              return 'bar!';
          }
          return null;
      }
  }
  
  $object = new Foo();
  echo $object-&gt;foo; // echoes "foo"
  echo $object-&gt;bar; // warning
  echo json_encode($object); // "{}"
  </code></pre>
  
  <p>I've tried implementing <code>IteratorAggregate</code> and <code>Serializable</code> for the class, but <code>json_encode</code> still doesn't see my magic properties. Since I am trying to encode an array of these objects, an <code>AsJSON()</code>-method on the class won't work either.</p>
  
  <p><strong>Update!</strong> It seems the question is easy to misunderstand. <em>How can I tell <code>json_encode</code> which "magic properties" exist?</em> <code>IteratorAggregate</code> didn't work.</p>
  
  <p>BTW: The <a href="http://www.php.net/manual/en/language.oop5.overloading.php" rel="noreferrer">term from the PHP documentation</a> is "dynamic entities". Whether or not the magic properties actually exist is arguing about semantics.</p>