Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:28435 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41539 invoked by uid 1010); 19 Mar 2007 19:33:42 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 41523 invoked from network); 19 Mar 2007 19:33:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2007 19:33:42 -0000 Authentication-Results: pb1.pair.com header.from=wez@omniti.com; sender-id=pass; domainkeys=good Authentication-Results: pb1.pair.com smtp.mail=wez@omniti.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain omniti.com designates 66.225.209.50 as permitted sender) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: wez@omniti.com X-Host-Fingerprint: 66.225.209.50 mail.omniti.com Linux 2.5 (sometimes 2.4) (4) Received: from [66.225.209.50] ([66.225.209.50:55719] helo=mail.omniti.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7B/B9-01604-295EEF54 for ; Mon, 19 Mar 2007 14:33:39 -0500 X-DKIM: Ecelerity dkim_sign implementing draft-ietf-dkim-base-00 DKIM-Signature: a=rsa-sha1; d=omniti.com; s=test; t=1174332815; c=simple/simple; q=dns; i=@omniti.com; h=From; b=ewFlQ/ZX5/y4KptbQPLoR5YeJ4zSYSqxzFoZJrPUoVME2pdBhUS0BFLFav8gsf0A Z7NOndBydm+kS+Jt+7sZRStwLB9JQZNg6Zn/RwHkShJ327UUTr97Qq0KzD3uwQS/ X-DomainKeys: Ecelerity dk_sign implementing draft-delany-domainkeys-base-01 DomainKey-Signature: q=dns; a=rsa-sha1; c=nofws; s=test; d=omniti.com; h=Authentication-Results:Received:In-Reply-To:References:Mime-Version:Content-Type:Message-Id:Cc:Content-Transfer-Encoding:From:Subject:Date:To:X-Mailer; b=MflwnN2qynn5AZwmc6OLJ4XKM+zFWIUb4znZaSy55F/cKqqRwxnS292k7+y99FTi NcAG/DHZ0vkUpk5RIKcu6rEiKO3iwoLOWAowDnNv0qepRk2yLqceXPPvD3y1iZyI Authentication-Results: mail.omniti.com smtp.user=wez; auth=pass (LOGIN) Received: from [66.80.117.2] ([66.80.117.2:54246] helo=[10.80.116.165]) by mail.omniti.com (ecelerity 2.1.1.12 r(14453)) with ESMTPSA (cipher=AES128-SHA) id DD/61-26454-885EEF54 for ; Mon, 19 Mar 2007 15:33:35 -0400 In-Reply-To: <45FEE396.7040905@caedmon.net> References: <86478A67-DCA2-4000-9EF0-DA4338E8389B@omniti.com> <45FDF031.4010508@zend.com> <45FE2312.1050506@zend.com> <45FED6D9.8030307@caedmon.net> <45FEDAE5.2010309@zend.com> <45FEE396.7040905@caedmon.net> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-ID: <0757BDEA-65F5-4123-B647-060DCA84B4B0@omniti.com> Cc: Stanislav Malyshev , internals@lists.php.net Content-Transfer-Encoding: 7bit Date: Mon, 19 Mar 2007 15:33:27 -0400 To: Sean Coates X-Mailer: Apple Mail (2.752.3) Subject: Re: [PHP-DEV] PATCH: anonymous functions in PHP From: wez@omniti.com (Wez Furlong) I've been thinking about this on and off today too. Something along the lines of the following is more in the PHP spirit: $ver = phpversion(); $fancyVer = function () { lexical $ver; return "PHP $ver"; }; Where "lexical" is a keyword that means "inherit this variable from the current lexical scope". I'm not suggesting that this is a good name for the keyword, it's just something that springs to mind. So, given some way to explicitly reference the scope where the function was "defined", what happens when you call $fancyVer after that scope has gone away: function doSomething() { $ver = phpversion(); return function () { lexical $ver; return "PHP $ver"; }; } $func = doSomething(); $func(); # the doSomething() scope (hash table) doesn't exist any more This could perhaps be solved by taking a reference to $ver when the function is bound, but I don't know enough about the ZE to understand the implications of that; it would probably require a bit more state tracking per zend_function so that we know that we need to do that step during binding. --Wez. On Mar 19, 2007, at 3:25 PM, Sean Coates wrote: >> If it would create anonymous function compile-time, it would be a big >> advantage to Wez's patch because then this function could be cached. >> Thinking about this, maybe it is the reason enough to do this even if >> it's not real closure. > > On mulling this over a bit more, other than quick one-off callbacks > (which would definitely benefit from avoiding compile on every > request), > one of the key reasons to use create_function() is actually to create > dynamic functions: > > $fancyVer = create_function('', 'return "PHP " . phpversion();'); > // could be optimized as: > $fancyVer = create_function('', 'return "PHP ' . phpversion() .'";'); > > (the latter only calls phpversion() at declaration, not each time the > lambda runs) > > Since phpversion() is available globally, this isn't a problem. But > what > happens if we want to use a variable, instead? > > $ver = phpversion(); > $fancyVer = create_function('', "return 'PHP $ver';"); > // this currently works ^ > > How would this be rewritten, though? > $ver = phpversion(); > $fancyVer = function () { return "PHP $ver"; }; > > where would $ver come from? the parent scope? the lambda's local > scope? > what if it's defined in both places? > > S > >