Skip to content

Instantly share code, notes, and snippets.

@aholmes
Created January 22, 2014 03:38
Show Gist options
  • Save aholmes/8553121 to your computer and use it in GitHub Desktop.
Save aholmes/8553121 to your computer and use it in GitHub Desktop.
<?php
class Result {
public $id;
public $foo = 'bar';
function __construct() {
$this->id = rand(0,9999);
}
}
class Result_Set {
private $result_set;
private $current_id = 0;
function __construct($result_set) {
$this->result_set = $result_set;
}
public function fetch() {
return isset($this->result_set[$this->current_id]) ? $this->result_set[$this->current_id++] : false;
}
}
class DB_Result implements Iterator {
private $result_set;
private $current_result;
function __construct($result_set) {
$this->result_set = $result_set;
}
function rewind() {
$this->current_result = $this->result_set->fetch();
}
function current() {
return $this->current_result;
}
function key() {
return $this->current_result->id;
}
function next() {
$this->current_result = $this->result_set->fetch();
}
function valid() {
return $this->current_result !== false;
}
}
$result_set = new Result_Set(array(
new Result(),
new Result(),
new Result(),
new Result(),
new Result(),
new Result(),
new Result(),
new Result(),
new Result(),
new Result()
));
$db_result = new DB_Result($result_set);
foreach($db_result as $row_id => $value) {
var_dump($row_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment