Skip to content

Instantly share code, notes, and snippets.

@sminnee
Last active November 5, 2015 23:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/40ac47b295bd2d9e067d to your computer and use it in GitHub Desktop.
Save sminnee/40ac47b295bd2d9e067d to your computer and use it in GitHub Desktop.
Custom generator classes with IteratorAggregate
<?php
class ForLoop implements IteratorAggregate
{
public $from = 0, $to, $step = 1;
function getIterator() {
for($i = $this->from; $i <= $this->to; $i += $this->step) {
yield $i;
}
}
}
$f = new ForLoop;
$f->from = 3;
$f->to = 9;
$f->step = 2;
foreach($f as $number) {
echo "$number things, ha ha ha!\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment