PHP < 5.4 的实现
<?php
if (!class_exists('CallbackFilterIterator')) {
class CallbackFilterIterator extends FilterIterator {
protected $callback;
// 在 PHP 5.4 中,“Closure”类型提示应为“callable”
public function __construct(Iterator $iterator, Closure $callback = null) {
$this->callback = $callback;
parent::__construct($iterator);
}
public function accept() {
return call_user_func(
$this->callback,
$this->current(),
$this->key(),
$this->getInnerIterator()
);
}
}
}
?>