Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Functions

Tortue Torche edited this page Mar 13, 2014 · 4 revisions

Available methods


Function limiters

Functions::after(function, $times)

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

Functions::cache(function)

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

Functions::once(function)

Allow a function to be called only once

$number = 0;
$increment = Functions::once(function() use(&$number) {
  $number++;
});

$increment(); // $number = 1
$increment(); // $number = 1

Functions::only(function, times)

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

Functions::throttle(function, seconds)

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