Last active
January 17, 2018 19:16
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The full MySQL client API and a few other useful PHP tools can be found here: https://github.com/johannesl/RealWorldPHP