Voting

: five plus one?
(Example: nine)

The Note You're Voting On

Relakuyae
13 years ago
Need a callback on an iterated value, but don't have PHP 5.4+? This makes is stupid easy:

<?php
class ArrayCallbackIterator extends ArrayIterator {
private
$callback;
public function
__construct($value, $callback) {
parent::__construct($value);
$this->callback = $callback;
}

public function
current() {
$value = parent::current();
return
call_user_func($this->callback, $value);
}
}
?>

You can use it pretty much exactly as the Array Iterator:

<?php
$iterator1
= new ArrayCallbackIterator($valueList, "callback_function");
$iterator2 = new ArrayCallbackIterator($valueList, array($object, "callback_class_method"));
?>

<< Back to user notes page

To Top