Created
May 26, 2015 14:42
-
-
Save countless-integers/3c54d316ae4d7b20bdbc to your computer and use it in GitHub Desktop.
PHP memoization test using static function var
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 | |
class Broom | |
{ | |
public function rememberMe() | |
{ | |
static $outcome; | |
if ($outcome) return $outcome; | |
return $outcome = rand(); | |
} | |
} | |
class Sponge extends \Broom | |
{ | |
} | |
$a = new \Broom; | |
var_dump($a->rememberMe(), $a->rememberMe(), $a->rememberMe()); | |
$b = new \Broom; | |
$c = new \Sponge; | |
var_dump((new \Broom)->rememberMe()); | |
var_dump($b->rememberMe(), $b->rememberMe()); | |
var_dump($c->rememberMe(), $c->rememberMe()); | |
var_dump((new \Broom)->rememberMe()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment