Skip to content

Instantly share code, notes, and snippets.

@vijinho
Created October 30, 2017 22:26
Show Gist options
  • Save vijinho/836f7dcb4cdafc8375181fcbcd636409 to your computer and use it in GitHub Desktop.
Save vijinho/836f7dcb4cdafc8375181fcbcd636409 to your computer and use it in GitHub Desktop.
php memoize()
<?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