Last active
November 5, 2015 23:20
-
-
Save sminnee/40ac47b295bd2d9e067d to your computer and use it in GitHub Desktop.
Custom generator classes with IteratorAggregate
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
<?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