我开发了一个接口和类来屏蔽 PDOStatement。
界面:
interface ResultSetInterface extends Iterator
{
public function count();
public function all();
}
班上:
class ResultSet implements ResultSetInterface
{
/**
* @var PDOStatement
*/
protected $pdoStatement;
protected $cursor = 0;
protected $current = null;
private $count = null;
public function __construct($pdoStatement)
{
$this->pdoStatement= $pdoStatement;
$this->count = $this->pdoStatement->rowCount();
}
public function rewind()
{
if ($this->cursor > 0) {
throw new Exception('Rewind is not possible');
}
$this->next();
}
public function valid()
{
return $this->cursor <= $this->count;
}
public function next()
{
$this->current = $this->pdoStatement->fetch();
$this->cursor++;
}
public function current()
{
return $this->current;
}
public function key()
{
}
public function count()
{
return $this->count;
}
public function all() {
$this->cursor = $this->count();
return $this->pdoStatement->fetchAll();
}
}
这工作正常。但我不确定如何使用实现 Iterator 类所必需的 key() 方法。有任何想法吗?