Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:43667 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6291 invoked from network); 13 Apr 2009 07:14:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2009 07:14:12 -0000 X-Host-Fingerprint: 66.183.89.177 unknown Received: from [66.183.89.177] ([66.183.89.177:6902] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8B/71-31755-246E2E94 for ; Mon, 13 Apr 2009 03:14:11 -0400 Message-ID: <8B.71.31755.246E2E94@pb1.pair.com> To: internals@lists.php.net Date: Mon, 13 Apr 2009 00:14:04 -0700 User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 References: <20090412025100.GA5493@klutometis.wikitex.org> In-Reply-To: <20090412025100.GA5493@klutometis.wikitex.org> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Posted-By: 66.183.89.177 Subject: Re: Closures and __FUNCTION__ From: frozenfire@thefrozenfire.com (Justin Martin) Hi Peter, If I recall correctly, you can use the 'use' keyword. $factorial = function($foo) use ($factorial) { $factorial($foo); } $factorial('Hello World!'); I'm still having issues compiling 5.3 on my system, so I haven't tested this. Thanks, Justin Martin Peter Danenberg wrote: > The original anonymous functions patch[1] contained support for > __FUNCTION__ as a recursion mechanism in closures, such that I should > be able to do something like this: > > $factorial = function($n) { > if ($n == 1) > return 1; > else > return $n * call_user_func(__FUNCTION__, $n - 1); > }; > > print $factorial(3); // => 6 > > It fails with a warning, though: > > Warning: call_user_func() expects parameter 1 to be a valid > callback, function '{closure}' not found or invalid function name > > Is there another recursion mechanism for closures besides something > like the $GLOBALS['factorial'] hack? > > Footnotes: > [1] http://marc.info/?l=php-internals&m=119995982228453&w=2 >