Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:38312 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66749 invoked from network); 17 Jun 2008 07:40:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jun 2008 07:40:37 -0000 Authentication-Results: pb1.pair.com header.from=chris_se@gmx.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=chris_se@gmx.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.net designates 213.165.64.20 as permitted sender) X-PHP-List-Original-Sender: chris_se@gmx.net X-Host-Fingerprint: 213.165.64.20 mail.gmx.net Linux 2.5 (sometimes 2.4) (4) Received: from [213.165.64.20] ([213.165.64.20:47683] helo=mail.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4E/B0-61252-47A67584 for ; Tue, 17 Jun 2008 03:40:37 -0400 Received: (qmail invoked by alias); 17 Jun 2008 07:40:31 -0000 Received: from p54A173C3.dip.t-dialin.net (EHLO chris-se.dyndns.org) [84.161.115.195] by mail.gmx.net (mp029) with SMTP; 17 Jun 2008 09:40:31 +0200 X-Authenticated: #186999 X-Provags-ID: V01U2FsdGVkX18u7wcIWuwud8PeR4H8kZb8WiFPNF1Au+7CZJfXe3 oYwhbzK/TWrNJ4 Received: from [192.168.0.175] (HSI-KBW-091-089-005-213.hsi2.kabelbw.de [91.89.5.213]) by chris-se.dyndns.org (Postfix) with ESMTP id 2EBFD10569 for ; Tue, 17 Jun 2008 09:29:51 +0200 (CEST) Message-ID: <48576A39.9040208@gmx.net> Date: Tue, 17 Jun 2008 09:39:37 +0200 User-Agent: Thunderbird 2.0.0.14 (X11/20080421) MIME-Version: 1.0 To: php-dev List References: <4856A547.3080801@gmx.net> <4856B752.1040500@gmx.net> <200806162257.24141.larry@garfieldtech.com> In-Reply-To: <200806162257.24141.larry@garfieldtech.com> X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 Subject: Re: [PHP-DEV] [PATCH] [RFC] Closures and lambda functions in PHP From: chris_se@gmx.net (Christian Seiler) Hi, > - I am a little confused about the OOP interaction. How does a function > become a public method of the class? To clarify: the "public method" ist just the internal representation of the lambda function and has *nothing* to do with the semantics of calling the lambda itself. The "method" only means that the lambda function defined inside another method can access the class members and "public" only means that the lambda function can still be called from outside the class. > class Example { > private $a = 2; > > function myMethod($b) { > $lambda = function() { > lexical $b; > return $this->a * $b; // This part I get > }; > return $lambda; > } > } > > $e = new Example(); > $lambda = $e->myMethod(); > $e->$lambda(5); No, that's not what my patch does. My patch does: class Example { private $a = 2; public function myMethod ($b) { return function () { lexical $b; return $this->a * $b; }; } } $e = new Example (); $lambda = $e->myMethod (4); var_dump ($lambda ()); // int(8) $lambda2 = $e->myMethod (6); var_dump ($lambda2 ()); // int(12) So esentially, it does not matter whether you define a lambda function inside a method or a function (or in global scope, for that matter), you always use it the same way. The in-class-method lambda function only has the additional advantage of being able to access the private and protected class members since *internally* it is treated like a public class method. > - Related to that, would it then be possible to add methods to a class at > runtime using lambda functions as the added methods? No. > If not, is that something that could reasonably be added here without > hosing performance (or at least doing so less than stacking __call() and > call_user_func_array() does)? If you want to add methods dynamically to classes, why not use the runkit extension? I really don't see a point in making lambda functions and closures something they are not. Regards, Christiaan