Skip to content

Instantly share code, notes, and snippets.

@ViliamKopecky
Last active June 12, 2018 08:21
Show Gist options
  • Save ViliamKopecky/f4bf375d57f7c528f73f959e0a5adf72 to your computer and use it in GitHub Desktop.
Save ViliamKopecky/f4bf375d57f7c528f73f959e0a5adf72 to your computer and use it in GitHub Desktop.
PHP cached calling functions proxy
<?php
/*
# Usage
# just prefix old function call with `Memoize::`
someFunction("foo", 123);
Memoize::someFunction("foo", 123);
*/
class Memoize
{
public static $enabled = true;
private static $cache = [];
public static function __callStatic($name, $arguments)
{
$key = serialize([$name, $arguments]);
if (isset(self::$cache[$key])) {
return self::$cache[$key];
}
return self::$cache[$key] = call_user_func_array($name, $arguments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment