Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69269 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31748 invoked from network); 23 Sep 2013 13:36:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Sep 2013 13:36:55 -0000 X-Host-Fingerprint: 80.4.21.210 cpc22-asfd3-2-0-cust209.1-2.cable.virginmedia.com Received: from [80.4.21.210] ([80.4.21.210:12918] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E1/50-29522-3F340425 for ; Mon, 23 Sep 2013 09:36:52 -0400 Message-ID: To: internals@lists.php.net Date: Mon, 23 Sep 2013 14:36:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 80.4.21.210 Subject: Re: RFC: Anonymous Classes From: krakjoe@php.net (Joe Watkins) On 09/23/2013 07:39 AM, Joe Watkins wrote: > Morning All, > > https://wiki.php.net/rfc/anonymous_classes > > I'd like to hear thoughts regarding the addition of anonymous > classes, patch included. > > Cheers > Joe Thanks chaps ... >> I am having a hard time picturing a real-world use-case for this. >> -Rasmus I think since you wrote that quite a few decent use cases have been suggested, testing, MVC (that wasn't on internals), throw away/private implementation of interfaces or abstracts. It has such a wide range of uses, I didn't think it necessary to point them out, as others have mentioned it's an established OO pattern to use objects of an anonymous class. >> I'd rather go for a `new BaseType` syntax, though: >> >> $subject->attach(new SplObserver() { >> function update(SplSubject $s) { >> printf("Got update from: %s\n" $subject); >> } >> ); Using class_name_reference in place of T_CLASS causes a conflict with existing syntax, which is the reason T_CLASS is accepted, additionally, using class is nicer shorthand for new stdClass {}. With that in mind, I haven't tried moving the arguments, this: (new class {definition} (arguments))->method() seems more natural/sensible than: (new class(arguments) {definition})->method() new class() just feels a bit wrong ... >> Frankly, constructors are not part of any interface. Besides a >> constructor >> is totally useless in an anonymous class as it will never be called >> after >> the object is created. The examples in the constructor were not meant as use cases but to show the syntax of use. If you are using an anonymous class to implement an interface then a constructor may not be required, however, removing the ability to declare a constructor is restrictive an unnecessary. Cheers Joe