title: php foreach() with a class array
body: <p>I need some help. I have a working class and I can use a foreach() to display the public variables:</p>
  
  <pre><code>class MyClass {
       public $a;
       public $b;
       function __construct($aX,$bX){
            $this-&gt;a = $aX;
            $this-&gt;b = $bX;
       }
  }
  
  $class = new MyClass(3,5);
  foreach($class as $key =&gt; $value) {
       print "$key =&gt; $value&lt;/br&gt;\n";
  }
  </code></pre>
  
  <p>produces:</p>
  
  <pre><code>a =&gt; 3
  b =&gt; 5
  </code></pre>
  
  <p>Now my problem arises when I want to have an array of MyClass:</p>
  
  <pre><code>class MyClass
  {
      public $a;
      public $b;
      function __construct($aX,$bX){
          $this-&gt;a = $aX;
          $this-&gt;b = $bX;
      }
  }
  
  $class = array(
       "odd"=&gt;new MyClass(3,5), 
       "even"=&gt;new MyClass(2,4)
       );
  foreach($class as $key =&gt; $value) {
      print "$key =&gt; $value&lt;/br&gt;\n";
  }
  </code></pre>
  
  <p>produces:</p>
  
  <pre><code>Catchable fatal error: Object of class MyClass could not be converted to string...
  </code></pre>
  
  <p>How can I loop through the all the elements of the $class array? Any help would be great!</p>