Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created February 5, 2012 11:04
Show Gist options
  • Save swvitaliy/1744771 to your computer and use it in GitHub Desktop.
Save swvitaliy/1744771 to your computer and use it in GitHub Desktop.
Proxy cacher method calls
<?php
/**
* proxycacher.php
* Proxy cacher method calls.
*
* Author: swvitaliy
*/
class ProxyCacher {
protected $origin = NULL;
private $cache = array();
function __call ($name, $args) {
if (is_object($this->origin) && method_exists($this->origin, $name)) {
$key = substr(sha1($name . serialize($args)), 0, 40);
return isset($this->cache[$key]) ? $this->cache[$key] :
$this->cache[$key] = call_user_func_array(array($this->origin, $name), $args);
}
// something else ...
}
}
/*class A {
function f ($v) { echo "A::a called\n"; return $v . "\n"; }
}
class B extends ProxyCacher {
function __construct () {
$this->origin = new A;
}
}
$b = new B;
echo $b->f('first');
echo $b->f('second');
echo $b->f('first');*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment