Created
October 30, 2017 22:26
-
-
Save vijinho/836f7dcb4cdafc8375181fcbcd636409 to your computer and use it in GitHub Desktop.
php memoize()
This file contains hidden or 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 | |
// https://www.youtube.com/watch?v=M3_xnTK6-pA?t=44m18s | |
// http://eddmann.com/posts/implementing-and-using-memoization-in-php/ | |
//$memoize = function ($function) { | |
function memoize($function) { | |
return function () use ($function) { | |
static $cache = []; | |
$args = func_get_args(); | |
$key = md5(serialize($args)); | |
if (empty($cache[$key])) { | |
$cache[$key] = call_user_func_array($function, $args); | |
} | |
return $cache[$key]; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment