This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Functions
Tortue Torche edited this page Mar 13, 2014
·
4 revisions
Available methods
Allow a function to be called only after $times times
$number = 0;
$increment = Functions::after(function() use(&$number) {
$number++;
}, 2);
$increment(); // $number = 0
$increment(); // $number = 0
$increment(); // $number = 1
$increment(); // $number = 2
Cache the result of a function so that it's only computed once (different arguments are taken into account)
$compute = Functions::cache(function() {
return microtime();
});
$compute(); // return 0.25139300 1138197510
$compute(); // return 0.25139300 1138197510
Allow a function to be called only once
$number = 0;
$increment = Functions::once(function() use(&$number) {
$number++;
});
$increment(); // $number = 1
$increment(); // $number = 1
Allow a function to be called only $times times
$number = 0;
$increment = Functions::only(function() use(&$number) {
$number++;
}, 2);
$increment(); // $number = 1
$increment(); // $number = 2
$increment(); // $number = 2
Allow a function to be called only every $seconds seconds
$number = 0;
$increment = Functions::throttle(function() use(&$number) {
$number++;
}, 1);
$increment(); // $number = 1
$increment(); // $number = 1
sleep(2);
$increment(); // $number = 2