Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46077 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98056 invoked from network); 18 Nov 2009 07:32:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Nov 2009 07:32:23 -0000 Authentication-Results: pb1.pair.com header.from=crocodile2u@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=crocodile2u@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.227 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: crocodile2u@gmail.com X-Host-Fingerprint: 209.85.220.227 mail-fx0-f227.google.com Received: from [209.85.220.227] ([209.85.220.227:38635] helo=mail-fx0-f227.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 54/F0-23283-503A30B4 for ; Wed, 18 Nov 2009 02:32:22 -0500 Received: by fxm27 with SMTP id 27so860347fxm.23 for ; Tue, 17 Nov 2009 23:32:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=IFyTuCcOUMW0RbwO8gvxiNBIK9tIDB9M+HlqYx30Hao=; b=TTWBkenr5IPyW2Pliu4q4Z/EELVNQGkRkB5M5TqHnJ42N94y9NZFxoNH9J4D7G2qiG MJSZEZa0XXeB++aHoIKyjhL0NBwrtFI+h+/dB27hO7hcNQz5Op6olMa9vOrn0wCWWvwQ 90wXVB6wDIfQ1GRerZPssvNa2pEaPphDpzDII= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=xspxjvjmaJGwjGKgngExjGn3e8rUhj9Z+Fd/CAXjkAIE1mM12PDURs2aQDwhXbDqmS F1E6GnsYQEPbbRrrw6ElQdQyvsThvnf9XisuqjYsvGY2WLVgNHzq9fNxrKLSNLop207h U6/3o9lR3SuVJPTXFLTBOTax4A5zJ1Bx+f6XU= MIME-Version: 1.0 Received: by 10.103.87.33 with SMTP id p33mr4956248mul.94.1258529521000; Tue, 17 Nov 2009 23:32:01 -0800 (PST) In-Reply-To: <4B02D7FA.3080206@easyflirt.com> References: <4B01A4C2.8030602@gmx.net> <4B0273E5.9000606@easyflirt.com> <4B02D7FA.3080206@easyflirt.com> Date: Wed, 18 Nov 2009 10:32:00 +0300 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Closures and $this From: crocodile2u@gmail.com (Victor Bolshov) Hi. Personally, I beleive that (A) approach is the best: "bind $this to the object scope at creation" and never change it, issue error when $this is used where not available. It also seems to me like this way it could be implemented with better performance, am I right? Dynamically changing $this introduces confusion. You can use $this->foo or $this->bar() inside a closure, but when $this dynamically changes - you cannot be sure that "foo" property or "bar" method is still there. Furthermore, dynamic approach introduces even more mess when thinking about availability of private/protected class members. Consider this code: class Foo { private $closure; function __construct() { /* It seems natural to me that, doing this assignment from within the c= lass, one can use private members inside the closure */ $this->closure =3D function() { $this->privateMethod(); }; } function setClosure($closure) { $this->closure =3D $closure; } function getClosure() { return $this->closure; } } $foo =3D new Foo(); $foo->getClosure()->__invoke();// #CALL-1 $foo->setClosure(function() { /* But is it good to use private members here? It seems like it would break encapsulation, and the information that the class was designed to hide - is now made available in public access, although indirect. */ $this->privateMethod(); }); $foo->getClosure()->__invoke();// #CALL-2 In the end, I would like PHP to issue error when $this is used in #CALL-2 but to continue working on #CALL-1. The (A) approach seems to me more predictable, more simple, it has less information the developer needs to keep in mind when using closures. 2009/11/17 Mathieu Suen : > Chris Stockton a =E9crit : >> >> Hello, >> >> On Tue, Nov 17, 2009 at 2:59 AM, Mathieu Suen >> wrote: >>> >>> Christian Seiler a =E9crit : >>>> >>>> Hi, >>>> >>>> since a few months have passed since the last discussion on this topic >>>> and perhaps people had time to gather some experience with the current >>>> closure implementation in PHP 5.3 I'd like to restart the debate on >>>> $this in closures and object extension. >> >> I believe that (D) wins my vote, and it convinces me twice. Once >> because I believe it is the most intuitive for users (A), and twice >> because I believe it is also the most useful (C) for users. In my >> opinion It seems the most "PHP" way. >> >> -Chris >> > > (D) is quite inconstant: > > $block =3D $foo->closur; > > $foo->closur(); > $block(); > > This would produce 2 different output. > For (C) here is my objection: > > Suppose that we are in a MVC pattern. > In a controller on could do: > > $model->onFailDo(function () {$this->reportError()}); > $model->save(); > > > In the model side: > > public function onFailDo($block) > { > =A0 =A0 =A0 =A0$this->signalFailure =3D $block > } > > public function save() > { > =A0 =A0 =A0 =A0//... Something wrong happen > =A0 =A0 =A0 =A0return $this->signalFailure(); > } > > If you dynamically bind the $this you are obliged to treat the error > reporting in the model which is not a good idea. Even worst you can't eve= n > catch up the error in the controller. > It also mean that the closure is getting tightly coupled with anyone who = is > using it. And passing closure to different object is like spreading > dependency over all the system. > > -- Mathieu Suen > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 Regards, Victor