Decorator classes

From: Date: Thu, 29 Dec 2016 13:12:33 +0000
Subject: Decorator classes
Groups: php.internals 
Request: Send a blank email to internals+get-97480@lists.php.net to get a copy of this message
Hello internals, I find that using the decorator pattern is very verbose and maybe something could be done about that, an article[1] I saw proposed a "decorates" keyword to solves this, here is how it might look like in PHP: interface Foo { public bar(); public taz(); } class EchoFooBar decorates Foo { public function bar() {
    $this->logger->debug("foo bar");
    return $this->decorated->bar();
} } class LoggedFooTaz decorates Foo { private $logger; public function __construct(Foo $decorated, Logger $logger) {
    $this->logger = $logger;
    $this->decorated = $decorated;
} public function taz() {
    $this->logger->debug("foo taz");
    return $this->decorated->taz();
} } * the constructor is optional and default to take the first argument and put it in $decorated * if the constructor is provided PHP check that $decorated is set and implements the decorated interface or class * all public methods not overridden are automatically proxied to the decorated object There is at least a few different ways to go about this, the previous example was just to give you a preview of what it could be. If there is interest I'd like to write an RFC for this :) What do you think about this ? thank you [1] https://dzone.com/articles/is-inheritance-dead

« previous php.internals (#97480) next »