Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75135 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81788 invoked from network); 30 Jun 2014 06:45:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jun 2014 06:45:58 -0000 Authentication-Results: pb1.pair.com header.from=smalyshev@sugarcrm.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=smalyshev@sugarcrm.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sugarcrm.com designates 108.166.43.67 as permitted sender) X-PHP-List-Original-Sender: smalyshev@sugarcrm.com X-Host-Fingerprint: 108.166.43.67 smtp67.ord1c.emailsrvr.com Linux 2.6 Received: from [108.166.43.67] ([108.166.43.67:37204] helo=smtp67.ord1c.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8A/60-07865-4A701B35 for ; Mon, 30 Jun 2014 02:45:57 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.relay.ord1c.emailsrvr.com (SMTP Server) with ESMTP id 985D83803CF; Mon, 30 Jun 2014 02:45:54 -0400 (EDT) X-Virus-Scanned: OK Received: by smtp1.relay.ord1c.emailsrvr.com (Authenticated sender: smalyshev-AT-sugarcrm.com) with ESMTPSA id 49E4E380322; Mon, 30 Jun 2014 02:45:54 -0400 (EDT) Message-ID: <53B107A1.6060200@sugarcrm.com> Date: Sun, 29 Jun 2014 23:45:53 -0700 Organization: SugarCRM User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: Sebastian Bergmann , "internals@lists.php.net" References: <53AEC4F0.8040307@php.net> In-Reply-To: <53AEC4F0.8040307@php.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Revisiting Anonymous Classes From: smalyshev@sugarcrm.com (Stas Malyshev) Hi! > I would like to propose the following syntax for declaring an > anonymous class that does not implement an interface or extend > a class: What would be the use case for such class? I can understand what is the use case for a just-in-time class that implement certain interface or extends certain class - mocks, stubs, handlers, etc. - e.g. widely used in Java in this way. However free-standing anonymous class, not connected to any hierarchy, does not seem to be very useful - why not have a real class then, or if it's too much trouble, an anon function? I know C# has them, but in that case it plays more of a role that stdclass or just array plays in PHP. OTOH, if you just make your anon class extend stdclass you have the same effect but without a special case that looks a bit weird to me. But maybe it's not a real problem since again you can do the same with stdclass. > $o = new AnInterface { Looks very similar to what Java does except Java does () and allows to pass ctor arguments. Are we assuming here anonymous classes do not have ctors, never call them or something else? A bit of bikeshedding: if we don't want to follow Java here, we could also do this: $o = class { public function method() { ... } ... } as AnInterface; This is (partially) stolen from Groovy syntax and follows roughly the same model as anon functions (definition without the name). Also, this is a bit like you define anonymous struct type in C and C-like languages. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/