Skip to content

Instantly share code, notes, and snippets.

@kivikakk
Created September 26, 2019 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kivikakk/42bc7f7d504d8256f7e5a13b7849e59d to your computer and use it in GitHub Desktop.
Save kivikakk/42bc7f7d504d8256f7e5a13b7849e59d to your computer and use it in GitHub Desktop.
<?php
function counter() {
$count = 0;
$incr = function() use (&$count) {
$count += 1;
return $count;
};
$reset = function() use (&$count) {
$count = 0;
};
return array($incr, $reset);
}
list($incr1, $reset1) = counter();
$incr1();
$incr1();
var_dump($incr1()); // 3
list($incr2, $reset2) = counter();
$incr2();
var_dump($incr2()); // 2
$reset2();
var_dump($incr2()); // 1
$reset1();
var_dump($incr1()); // 1
var_dump($incr2()); // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment