Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:69337 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96017 invoked from network); 25 Sep 2013 13:43:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Sep 2013 13:43:44 -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:28995] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 21/A0-27377-098E2425 for ; Wed, 25 Sep 2013 09:43:44 -0400 To: internals@lists.php.net,Terence Copestake Message-ID: <5242E88D.90407@php.net> Date: Wed, 25 Sep 2013 14:43:41 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130625 Thunderbird/17.0.7 MIME-Version: 1.0 References: <5241F11C.5080707@php.net> <008301ceb967$b49ab190$1dd014b0$@tutteli.ch> <524205D8.8000608@php.net> <5242ADAE.2080007@php.net> 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: [PHP-DEV] RFC: Anonymous Classes From: krakjoe@php.net (Joe Watkins) On 09/25/2013 02:02 PM, Terence Copestake wrote: >> 1) Anonymous classes in PHP would support a constructor, so I don't see >> the need for use to be utilized here, it would just clutter declarations >> and the patch. > > > This works, but it's more effort for the programmer and arguably just > moving the "clutter" from the declaration to the constructor. > > e.g. > > (new class ... > protected $value; > public function __construct($value) > { > $this->value = $value; > } > > public function doSomething() > { > echo $this->value; > })($val) > > vs. > > (new class ... > public function doSomething() use ($val) > { > echo $val; > }) > > It gets even uglier for passing by reference. > > >> 2) Again, constructors can be used, and is less confusing than introducing >> a new variable, but I'm open to persuasion if the idea is liked in general >> ... > > > This is fine when working with only a handful of values, but what about > needing access to a large number of values? What about making (especially > protected) method calls within the anon class? > > Along the same vein, why did we even bother having "use" and $this for > closures, if you can after all just pass everything as arguments? > > If there are realistic and elegant alternative solutions to the problems > presented, that's fair enough. Rejecting something because it's not as > simple to implement and you can hack around it in user code anyway... > that's a dangerous way to go about your business. > The first example doesn't make good sense: > (new class ... > protected $value; > public function __construct($value) > { > $this->value = $value; > } > > public function doSomething() > { > echo $this->value; > })($val) If $value is protected then how are you passing it in from outside as $val ? If your anonymous class needs access to protected methods (which in turn might want to read private members) then there's already an established way of declaring that: dice = $dice; } protected function protectedMethod() { return $this->dice; } public function publicMethod() { return new class extends Outer { public function forwardToProtectedMethod() { $this->protectedMethod(); } }($this); } } var_dump((new Outer($_SERVER))->publicMethod()); ?> This works now, as you expect it too ... not elegant enough ?? you don't think use() everywhere might be a little bit much ?? Lets try to keep it as simple as possible, I think ... Cheers