Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49200 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10841 invoked from network); 3 Aug 2010 18:12:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Aug 2010 18:12:33 -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.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:46169] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/39-61854-D0C585C4 for ; Tue, 03 Aug 2010 14:12:31 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 0BFAE7000E53 for ; Tue, 3 Aug 2010 19:12:26 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id 5KBWbAE9vJth for ; Tue, 3 Aug 2010 19:12:25 +0100 (WEST) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp1.ist.utl.pt (Postfix) with ESMTP id C53DC7000456 for ; Tue, 3 Aug 2010 19:12:25 +0100 (WEST) Received: from cataphract-old.dulce.lo.geleia.net (a83-132-178-216.cpe.netcabo.pt [83.132.178.216]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id 998522008D4A for ; Tue, 3 Aug 2010 19:12:25 +0100 (WEST) Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes To: "PHP Internals" Date: Tue, 03 Aug 2010 19:12:22 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Organization: =?iso-8859-15?Q?N=FAcleo_de_Eng=2E_Biom=E9dica_?= =?iso-8859-15?Q?do_IST?= Message-ID: User-Agent: Opera Mail/10.60 (Win32) Subject: Closures, rebinding and scopes From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") Hi Even though one cannot bind a static closure to an instance, one can rebind its scope with the Closure::bind/Closure::bindTo: class foo { static protected $field = "foo_field"; static function getClosure() { return static function () { //causes a crash (fbc already freed) in call after rebinding, //so let's not use it //echo get_called_class(), "\n"; echo static::$field, "\n"; }; } } class subFoo { static protected $field = "subfoo_field"; } $c = foo::getClosure(); $c(); //foo_field $c = $c->bindTo(new subFoo()); $c(); //subfoo_field It doesn't make sense to require an instance in order to change the scope of a static closure, so perhaps better options would be: 1. Change Closure::bind/Closure::bindTo so that they accept a class name for static closures and an instance otherwise 2. Provide a method to change simultaneously the (calling) scope and the called scope 3. Provide separate methods to change the called scope and the scope I think either 2 or 3 would be better than 1 because the fact the rebinding a closure changes its scope is not a great idea. Consider: class foo { private $field = "foo"; function getClosure() { return function () { echo $this->field, "\n"; }; } } class subFoo extends foo {} $f = new subFoo(); $g = new subFoo(); $c = $f->getClosure(); $c(); //foo $c = $c->bindTo($g); //or even $c->bindTo($f) $c(); //fails Thoughts? -- Gustavo Lopes