Created
September 26, 2019 00:50
-
-
Save kivikakk/42bc7f7d504d8256f7e5a13b7849e59d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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