Skip to content

Instantly share code, notes, and snippets.

@johannesl
Last active January 17, 2018 19:16
Show Gist options
  • Save johannesl/bfd25b0e665c6c218404140c3f66348a to your computer and use it in GitHub Desktop.
Save johannesl/bfd25b0e665c6c218404140c3f66348a to your computer and use it in GitHub Desktop.
Sample PHP iterator. Took a while to gather all the pieces so might be useful for someone else. I made this test in order to extend my easiermysql.php with a version of mSelectRows() that supports retrieving big row sets from MySQL (more than what's feasible storing in an array).
<?
class mIterator implements Iterator {
public function __construct( $data ) {
$this->data = $data;
}
public function valid () {
if ($this->row) return true;
return false;
}
public function current () {
return $this->row;
}
public function key () {
return $this->key;
}
public function next () {
$this->row = array_pop($this->data);
$this->key++;
}
public function rewind () {
$this->key = -1;
$this->next();
}
}
$rows = new mIterator( array( 'One', 'Two', 'Three' ) );
foreach ($rows as $key => $value) {
echo $key ."=". $value ."\n";
}
?>
@johannesl
Copy link
Author

The full MySQL client API and a few other useful PHP tools can be found here: https://github.com/johannesl/RealWorldPHP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment