Created
February 5, 2012 11:04
-
-
Save swvitaliy/1744771 to your computer and use it in GitHub Desktop.
Proxy cacher method calls
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 | |
/** | |
* 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