Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97483 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16028 invoked from network); 29 Dec 2016 14:19:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Dec 2016 14:19:15 -0000 Authentication-Results: pb1.pair.com header.from=mathieu@rochette.cc; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mathieu@rochette.cc; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain rochette.cc designates 62.210.206.189 as permitted sender) X-PHP-List-Original-Sender: mathieu@rochette.cc X-Host-Fingerprint: 62.210.206.189 texthtml.net Received: from [62.210.206.189] ([62.210.206.189:39432] helo=texthtml.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A2/14-04761-16B15685 for ; Thu, 29 Dec 2016 09:19:14 -0500 Received: by texthtml.net (Postfix, from userid 65534) id C06684D8; Thu, 29 Dec 2016 14:19:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on a0ff274cda9f X-Spam-Level: X-Spam-Status: No, score=-1.0 required=4.0 tests=ALL_TRUSTED,HTML_MESSAGE autolearn=ham autolearn_force=no version=3.4.1 Received: from [192.168.1.130] (stunnel_mail_1.mail_default [172.29.0.4]) (Authenticated sender: mathieu@texthtml.net) by texthtml.net (Postfix) with ESMTPA id 3B49B4D6; Thu, 29 Dec 2016 14:18:54 +0000 (UTC) To: Alexander Lisachenko Cc: PHP internals References: <2aecc3f2-5e9d-907f-029b-5c60eb134b33@rochette.cc> Message-ID: Date: Thu, 29 Dec 2016 15:18:53 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:51.0) Gecko/20100101 Thunderbird/51.0a2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------4B0BEF5D21534DF2E9D1FDDE" Content-Language: en-US Subject: Re: [PHP-DEV] Decorator classes From: mathieu@rochette.cc (Mathieu Rochette) --------------4B0BEF5D21534DF2E9D1FDDE Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 29/12/2016 14:44, Alexander Lisachenko wrote: > > 2016-12-29 16:12 GMT+03:00 Mathieu Rochette >: > > 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: > > > * 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 ? > > > Hi! I think that language doesn't need any additional keywords for > that. What you describe is pure class inheritance and it's already > available in the language. It is not class inheritance, as you can see in my example, the decorator is on an interface. one point of the decorator pattern is that you don't need to know which class (or subclass) you are decorating. any object implementing the interface (or extending the class if it's a class you are decorating) can be decorated. > > To be honest I use class inheritance in Go! AOP to define decorators > because of some nice features: > > 1. No overhead for calling non-decorated method (we can wrap only > required methods) > I think PHP could be smart enough to do that too for non-decorated methods with the decorates keyword > > 1. Only one single instance of class, whereas traditional proxy and > decorator pattern will use two instances (one for the original > instance itself and one more instance for proxy/decorator) > I'm not sure decorator is appropriate here, in the article you are not decorating[1] your class; you are outsourcing the behavior but it's not the same > > What your want can be implemented with the help of one single aspect, > applied to your codebase. Then framework will automatically create > decorators for every required class in your system. For logging > decorator example see my blogpost here: > http://go.aopphp.com/blog/2013/07/21/implementing-logging-aspect-with-doctrine-annotations/ > [1] https://en.wikipedia.org/wiki/Decorator_pattern --------------4B0BEF5D21534DF2E9D1FDDE--