Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69312 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91503 invoked from network); 24 Sep 2013 12:13:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Sep 2013 12:13:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=linepogl@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=linepogl@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.44 as permitted sender) X-PHP-List-Original-Sender: linepogl@gmail.com X-Host-Fingerprint: 209.85.220.44 mail-pa0-f44.google.com Received: from [209.85.220.44] ([209.85.220.44:51630] helo=mail-pa0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/20-24892-FD181425 for ; Tue, 24 Sep 2013 08:13:20 -0400 Received: by mail-pa0-f44.google.com with SMTP id lf10so3607734pab.31 for ; Tue, 24 Sep 2013 05:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=TCue1ROziOTbQP+qm75tc3Na7CNwLRZi3vlDtaGpdo8=; b=INk01XJByFahaYigRdjFZaH2XxlY3Vsnm75r8gPUHqZu4NBWzYn+mFN7R5FQgX7fAx w9blFhapaalGJK8BQADRp6DPRVmnZXOg21kbu3iyGITF9F23UBbHWwA/2oLhhvSy4TYI rqiR3Kdsr+AdeQ0Ljo4XO2yq9umjYq1pUm0EJ/5l+kWPSalAYAOxIGt8oBD9bvTiWFU2 4uLu2POIoN25fnfSya5i9SjeSgZwaN1MdwYMgHHcSiAl5Mzld1u/wP2kJBXAohd0HRMP 7LKJRsaiJffFKLNmtd6fc9iR9DywaBd5ua19lGExQ5pgryfRPqZuyqlje6QT24JmwTWf E6yA== X-Received: by 10.66.118.233 with SMTP id kp9mr4401542pab.182.1380024797031; Tue, 24 Sep 2013 05:13:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.70.3.163 with HTTP; Tue, 24 Sep 2013 05:12:56 -0700 (PDT) In-Reply-To: References: Date: Tue, 24 Sep 2013 14:12:56 +0200 Message-ID: To: David Soria Parra Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=e89a8ffbac6dcef35d04e7200f46 Subject: Re: [PHP-DEV] RFC: Anonymous Classes From: linepogl@gmail.com (Lazare Inepologlou) --e89a8ffbac6dcef35d04e7200f46 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2013/9/24 David Soria Parra > Lazare Inepologlou schrieb: > > I use anonymous classes very frequently in Java and in C#, and I would > say > > that they are quite useful. However the examples given in the RFC are > > really bad ones. Why on earth would you need a constructor for an > anonymous > > class? Anonymous classes are used to implement quickly some interface. > > Frankly, constructors are not part of any interface. Besides a > constructor > > is totally useless in an anonymous class as it will never be called aft= er > > the object is created. > > The need for anonymous classes in Java mostly comes from implementing > interfaces on demand due to the lack of lamdas and the requirements of > a strongly typed language. PHP has lambdas and it doesn't require you > to match typing. Therefore I think the preferred way for most of the > use cases is to use a lambda or if a bigger implementation is necessary, > to give it a name. Due to namespacing you don't pollute the global scope > anyway. Therefore I don't see much reason left as to why add anonymous > classes and I really have a hard time finding a use case, except for tryi= ng > to write PHP as you would write Java (which most likely is a bad idea). > > There are many use cases where anonymous classes are useful, even in the presence of lambdas. I use them quite often when dealing with graphical interfaces and templates. Here is an example: abstract class MyFancyHtmlListView extends UI { protected function IsHeaderVisible(){ return true; } protected function GetListItemMenu(){ return null; } protected function OnItemClick( $item ){ } protected abstract function RenderListItem( $item ); public function Render(){ // echo ... } } With anonymous classes we could do something like this: The biggest advantage is that a missing RenderListItem could be statically verified. It is just a pattern that follows a different way of thinking: Instead of having a list of parameters (including lambdas), we have standard methods that take advantage of all the nice properties of OOP such as abstraction, inheritance and polymorphism. Lazare INEPOLOGLOU Ing=C3=A9nieur Logiciel --e89a8ffbac6dcef35d04e7200f46--