ループのコードは次のとおりです。クラスでは、私がここでIteratorインターフェイス を実装に問題がある
class User_Model_Users implements Iterator, Countable
{
protected $_count;
protected $_gateway;
protected $_resultSet;
public function __construct($results, $gateway)
{
$this->setGateway($gateway);
$this->_resultSet = $results;
}
public function setGateway(User_Model_UserGateway $gateway)
{
$this->_gateway = $gateway;
return $this;
}
public function getGateway()
{
return $this->_gateway;
}
public function count()
{
if (null === $this->_count) {
$this->_count = count($this->_resultSet);
}
return $this->_count;
}
public function current()
{
if ($this->_resultSet instanceof Iterator) {
$key = $this->_resultSet->key();
} else {
$key = key($this->_resultSet);
}
$result = $this->_resultSet [$key];
if (!$result instanceof User_Model_User) {
$gateway = $this->getGateway();
$result = $gateway->createUser($result);
$this->_resultSet [$key] = $result;
}
return $result;
}
public function key()
{
return key($this->_resultSet);
}
public function next()
{
return next($this->_resultSet);
}
public function rewind()
{
return reset($this->_resultSet);
}
public function valid()
{
return (bool) $this->current();
}
}
$結果として、私ははZend_Db_Table_Rowsetを提供しますが、それはまた、他のオブジェクトや配列にすることができます。 foreachループで動作するようにこのコードを修正するにはどうすればよいですか? 無限ループなのでエラーはありません。
「はまた、他のオブジェクトまたは配列とすることができる」 - この場合の例スクリプト+データと期待される結果はいいだろう。 – VolkerK
for this Zend_Db_Table_Rowsetでは動作しません。私はこの場合を解決したいと思います – greg606
具体的にあなたの質問は何ですか?修正が必要だと思いますか?どのエラーが出ますか? 「うまくいかない」とは何がうまくいかないとは言いません。 – hakre