Created
February 18, 2013 21:31
-
-
Save komita1981/4980951 to your computer and use it in GitHub Desktop.
FilterIterator dummy example
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 FilterExample extends FilterIterator | |
{ | |
protected $number_type; | |
public function __construct($iterator, $number_type) | |
{ | |
parent::__construct($iterator); | |
$this->number_type = $number_type; | |
} | |
public function accept() | |
{ | |
switch ($this->number_type) | |
{ | |
case 'even': | |
return ! ($this->current() % 2); | |
break; | |
case 'odd': | |
return ($this->current() % 2); | |
default: | |
return false; | |
break; | |
} | |
return false; | |
} | |
} | |
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); | |
$filtered = new FilterExample(new ArrayIterator($numbers), 'even'); | |
foreach ($filtered as $variable => $value) | |
{ | |
echo $value.'<br/>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment