12 Aug 2012 20:17
[PHP-DEV] Decorators Revisited
Anthony Ferrara <ircmaxell <at> gmail.com>
2012-08-12 18:17:24 GMT
2012-08-12 18:17:24 GMT
Hey all
I've posted before about adding the ability to do dynamic decorators
before. I think I have come up with a method to do so in core.
Basically, the problem is that I can't create a decorator for a class at
all. I would have to extend that class (with all of the coupling issues
that brings). I can create a decorator for interfaces, but I'd need to list
proxy methods for each and every interface combination in each decorator.
This leads to tons of boilerplate issues. For example:
class CachableFooDecorator implements foo {
protected $parent;
public function __construct(Foo $obj) {
$this->parent = $obj;
}
public function __call($method, $args) {
return call_user_func_array(array($this->parent, $method), $args);
}
public function method1($a, $b) {
return $this->parent->method1($a, $b);
}
public function method2($a) {
if (!$this->hasCache('method2', $a)) {
$ret = $this->parent->method2($a);
$this->setCache('method2', $a, $ret);
}
return $this->getCache('method2', $a);
}
}
(Continue reading)
RSS Feed