Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49337 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24174 invoked from network); 11 Aug 2010 14:29:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Aug 2010 14:29:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.22 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.22 smtp2.ist.utl.pt Linux 2.6 Received: from [193.136.128.22] ([193.136.128.22:35360] helo=smtp2.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4D/F3-01618-DD3B26C4 for ; Wed, 11 Aug 2010 10:29:50 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 02DE570042B7; Wed, 11 Aug 2010 15:29:46 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp2.ist.utl.pt ([127.0.0.1]) by localhost (smtp2.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id S-FhSjku60Ge; Wed, 11 Aug 2010 15:29:45 +0100 (WEST) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp2.ist.utl.pt (Postfix) with ESMTP id A934A7004251; Wed, 11 Aug 2010 15:29:45 +0100 (WEST) Received: from cataphract-old.dulce.lo.geleia.net (52.152.108.93.rev.vodafone.pt [93.108.152.52]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id 855B42008D32; Wed, 11 Aug 2010 15:29:45 +0100 (WEST) Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes To: =?iso-8859-15?Q?Johannes_Schl=FCter?= Cc: "internals@lists.php.net" References: <1281535067.1132.15.camel@guybrush> Date: Wed, 11 Aug 2010 15:29:50 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: Quoted-Printable Organization: =?iso-8859-15?Q?N=FAcleo_de_Eng=2E_Biom=E9dica_?= =?iso-8859-15?Q?do_IST?= Message-ID: In-Reply-To: <1281535067.1132.15.camel@guybrush> User-Agent: Opera Mail/10.60 (Win32) Subject: Re: [PHP-DEV] Closures as methods (and Closure::bind) From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Wed, 11 Aug 2010 14:57:47 +0100, Johannes Schl=FCter = wrote: > On Wed, 2010-08-11 at 14:38 +0100, Gustavo Lopes wrote: >> I've updated the wiki page for "Closures with objects extension" with= >> things that are in "Proposal A with modification"s but are not = >> implemented: >> >> http://wiki.php.net/rfc/closures/object-extension#status_as_of_august= _10_2010 >> >> I propose an implementation of "closures stored in properties used as= >> methods", as in: >> >> $this->prop =3D function () { ...} >> $this->prop(); >> >> A few issues that may merit discussion (copied from the wiki page): > > A few more things coming to mind without much thought: > > * What if both a method and a property with the name exist? An existing method has priority over closure-as-method, just like it has= = priority over __call. I implemented what was in the modified proposal A.= = Personally, I think __call should have priority over closure-as-method s= o = as to not to break BC. > * What about allowing properties with function names as strings = or > array($obj_or_class, 'method'), won't that be needed for being= > consistent with local variables? Well, you cannot do "$a =3D 'phpinfo'; $a();" as well, so it's consisten= t = with that. I think what you refer to would only make sense if that were = = allowed. > * In the array($object, 'method') case: What's the scope= > for $this? call_user_func(array($object, 'method')) is the same as $object->method(= ), = the calling scope is that whatever scope is defined for the closure. The= = value of $this is the object that's bound to the closure; an E_WARNING = notice is raised if $this !=3D=3D $object (as mentioned in the proposal)= . class A { public $prop; } $a1 =3D new A; $a2 =3D new A; $a1->prop =3D Closure::bind(function () { var_dump($this); }, $a2); call_user_func(array($a1, 'prop')); gives: Warning: Closure called as method but bound object differs from containi= ng = object in ... object(A)#2 (1) { ["prop"]=3D> NULL } > I don#t have an opinion on this feature, yet, I like the current > class-based object model as reading code is relatively simple, with th= is > addition (and the fact that you can create properties on the fly) we > create a powerful tool for really hard to read code. > We could forbid using dynamic properties for this functionality, though = = I'd say it's arguably useful. -- = Gustavo Lopes