Skip to content

Instantly share code, notes, and snippets.

@lastguest
Created May 2, 2012 14:40
Show Gist options
  • Save lastguest/2577049 to your computer and use it in GitHub Desktop.
Save lastguest/2577049 to your computer and use it in GitHub Desktop.
PHP Memoization
<?php
function memoize(&$callback){
return $callback = function() use ($callback,$cache){
static $cache = array();
$args = func_get_args();
$hash_key = implode(',',$args);
return isset($cache[$hash_key])?$cache[$hash_key]:$cache[$hash_key]=call_user_func_array($callback, $args);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment