Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:39180 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84249 invoked from network); 22 Jul 2008 12:17:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Jul 2008 12:17:04 -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:54270] helo=mail.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0C/17-46193-EBFC5884 for ; Tue, 22 Jul 2008 08:17:03 -0400 Received: (qmail invoked by alias); 22 Jul 2008 12:16:59 -0000 Received: from p54A16A22.dip.t-dialin.net (EHLO chris-se.dyndns.org) [84.161.106.34] by mail.gmx.net (mp016) with SMTP; 22 Jul 2008 14:16:59 +0200 X-Authenticated: #186999 X-Provags-ID: V01U2FsdGVkX198KGA+Flt8IPnCLOvWkrLiGUfFtw6uwSvs74ad7V g+KIxWoxOYjNvp Received: from [192.168.100.13] (cobalt.seiler.lan [192.168.100.13]) by chris-se.dyndns.org (Postfix) with ESMTP id 31DA518F8C; Tue, 22 Jul 2008 13:45:06 +0200 (CEST) Message-ID: <4885CF6B.5060600@gmx.net> Date: Tue, 22 Jul 2008 14:15:39 +0200 User-Agent: Thunderbird 2.0.0.14 (X11/20080421) MIME-Version: 1.0 To: Lukas Kahwe Smith CC: PHP Developers Mailing List , kalle@zesix.com References: <8A5D4032-1FC4-44BB-90F3-61B802F10159@pooteeweet.org> <6BD46F4A-2733-4160-9A3E-AD49F3002865@pooteeweet.org> In-Reply-To: <6BD46F4A-2733-4160-9A3E-AD49F3002865@pooteeweet.org> X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.53 Subject: Re: [PHP-DEV] closures questions From: chris_se@gmx.net (Christian Seiler) Hi Lukas, >> 1) Closures on class properties just don't work, the only way to do it is >> to do something like: >> >> $c = $a->b; >> $c(); >> >> Calling: $a->b(); will result in method A::B() does not exists. Yes, that's expected behaviour (we had a few comments on this on the list). Compare this to, for example: function hello () { echo "Hello World!\n"; } $a->b = 'hello'; $c = $a->b; $a->b (); // undefined method $c (); // works But, as closures implement the __invoke method, it's possible to do this: $a->b->__invoke (); or, of course call_user_func ($a->b); (which works with all types of callbacks, even non-closures) > so do we even want the toString() method? Personally, I don't care. >> 4) var_export() and Closures is useless What would you suggest? Having var_export return the function body for the closures? But what about bound variables? Well, perhaps in the future you could even export all the bound variables. But for know to keep it simple I'd say it's best that closures can't be exported. >> 5) Its impossible to clone a closure using the cloning keyword: >> >> $a = function(){}; >> $b = clone $a; That's intended behaviour. What would be the clone of a closure? Especially with bound variables? This would allow for all sorts of weird behaviour. Take, for example, the following: class Foo { private $someProperty; function getPrinter ($outputDevice) { return function ($text) use ($outputDevice) { $outputDevice->print ($this->someProperty, $text); }; } } How would you clone that? There are two bound variables in this closure: $this and $outputDevice. Do you clone them both? Do you clone only $this? Do you clone only $outputDevice? If you only clone the closure object itself, it won't change the behaviour from simply creating another reference to it... If one *really* needs a clonable *and* callable object, write a class that implements __clone and __invoke. Then the programmer can control the exact semantics. Everything else would simply be extremely confusing. And, if I take another language such as Python for example, there you can't clone closures either, y = copy.deepcopy (closure) returns the same object. >> This makes it hard to make a copy of the closure because of objects >> always are >> passed by reference. > > I guess this is a draw back from the OO approach (rather than the > original resource approach), but solvable? No, the original resource approach was just the same. A resource is basically an integer value which is then used to look up some specific data. You can't clone a resource either. Regards, Christian