Skip to content

Instantly share code, notes, and snippets.

@komita1981
Created February 18, 2013 21:31
Show Gist options
  • Save komita1981/4980951 to your computer and use it in GitHub Desktop.
Save komita1981/4980951 to your computer and use it in GitHub Desktop.
FilterIterator dummy example
<?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