title: Is there are way to foreach on object which class implements Iterator in PHP?
body: <p>I tried to modify example from php.net for trying to implement Java/C#-like collection where keys may be objects (<a href="http://php.net/manual/en/language.oop5.iterations.php" rel="nofollow">http://php.net/manual/en/language.oop5.iterations.php</a>, Example # 2):</p>
  
  <pre><code>&lt;?php
  class Test {
          private $_n;
          public function __construct($n) {
                $this-&gt;_n = $n;  
          }
          public function getN() {
                  return $this-&gt;_n;
          }
  }
  class MyIterator implements Iterator
  {
      private $var = array();
  
     // code from php.net ....
  
      public function key() 
      {
          $var = key($this-&gt;var);
          echo "key: $var\n";
  
          return new Test($var);
      }
  
      // code from php.net...
  
  }
  
  $values = array(1,2,3);
  $it = new MyIterator($values);
  
  foreach ($it as $a =&gt; $b) {
      print $a-&gt;getN() . ": $b\n";
  }
  </code></pre>
  
  <p>But I have notices like that:</p>
  
  <blockquote>
    <p>Warning: Illegal type returned from MyIterator::key()</p>
  </blockquote>
  
  <p>How can I to fix it?</p>