Re: Decorator classes
| From: | Mathieu Rochette | Date: | Thu, 29 Dec 2016 14:27:30 +0000 |
| Subject: | Re: Decorator classes | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-97485@lists.php.net to get a copy of this message | ||
On 29/12/2016 14:18, Marco Pivetta wrote:
On Thu, Dec 29, 2016 at 2:12 PM, Mathieu Rochette <mathieu@rochette.cc <mailto:mathieu@rochette.cc>> wrote:I also think that most of the time using the decorator pattern helps following the single responsibility principle also, this would add one more benefit : methods that are not override could probably be optimized by the PHP engine (avoid one userland proxy call)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 objectThere 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 <https://dzone.com/articles/is-inheritance-dead>-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.phpJust my 2 cents: Too much magic syntactic sugar, whereas you are just saving very little time while implementing a few method stubs that take.. yeah it's mostly syntactic sugar, I think it would encourage developer to use this pattern instead of inheritance when appropriate.
Yet, in order to achieve that, you added a huge amount of complexity for all the tooling that relies on the AST, including PHP itself. . Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/