キーがオブジェクトである可能性のあるJava/C#のようなコレクションを実装するためにphp.netの例を変更しようとしました(http://php.net/manual/en/language.oop5.iterations.php、例#2):
<?php
class Test {
private $_n;
public function __construct($n) {
$this->_n = $n;
}
public function getN() {
return $this->_n;
}
}
class MyIterator implements Iterator
{
private $var = array();
// code from php.net ....
public function key()
{
$var = key($this->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 => $b) {
print $a->getN() . ": $b\n";
}
しかし、私はそのような通知を持っています:
警告:MyIterator :: key()から不正なタイプが返されました
どうすれば修正できますか?