Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:43315 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55245 invoked from network); 11 Mar 2009 20:25:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2009 20:25:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=nate@cakephp.org; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nate@cakephp.org; sender-id=pass Received-SPF: pass (pb1.pair.com: domain cakephp.org designates 209.62.120.18 as permitted sender) X-PHP-List-Original-Sender: nate@cakephp.org X-Host-Fingerprint: 209.62.120.18 unknown Received: from [209.62.120.18] ([209.62.120.18:51970] helo=mail.cakephp.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1D/B9-09289-83E18B94 for ; Wed, 11 Mar 2009 15:25:29 -0500 Received: from [10.0.1.3] (user-0cev9h6.cable.mindspring.com [24.239.166.38]) (Authenticated sender: nate@cakephp.org) by mail.cakephp.org (Postfix) with ESMTPSA id 3C5BB50808 for ; Wed, 11 Mar 2009 15:26:30 -0500 (CDT) Message-ID: <6E2164EC-F333-4E89-8EBD-6618BA321CD6@cakephp.org> To: PHP Development Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v930.3) Date: Wed, 11 Mar 2009 16:25:23 -0400 X-Mailer: Apple Mail (2.930.3) Subject: Questions on closures and visibility From: nate@cakephp.org (Nate Abele) Hi all, Since around alpha1, I've been doing some experimenting with closures, most recently in the form of a filter chain implementation, and I've discovered that as of beta1 (as was noted in the "removal-of-$this" RFC) that there is no way to access private or protected members of any class inside a closure, regardless of where it is defined. I realize that the question of implicit $this support for closures has already been discussed and decided upon temporarily (and I certainly have no wish to rehash it again), however, it seems logical that even without being bound to an object, closures should still inherit the scope in which they're defined. Otherwise, this creates a confusing inconsistency within the language. Consider the following: class Exposed { private $_property; public function __construct($data = null) { $this->_property = $data; } public function getProtected($self, $property) { return $self->$property; } } $object1 = new Exposed('secret'); $object2 = new Exposed(); // successfully echos "secret" echo $object2->getProtected($object1, '_property')); Because of PHP's scope and visibility rules, this works even though $object1 and $object2 are only associated by common parentage. While this may have no technical significance, since a closure is actually it's own class, in practical terms it puts a damper on the usefulness of the implementation, and is a bit of a "WTF", particularly when considered in context with "use", which allows closures to inherit and even modify variables in the declaring scope, regardless of where they are called. Again, to clarify, I am not arguing for $this support to be added back to closures (though if a consensus could be reached before 5.3 final, that'd be great, too :-)). I am simply suggesting that the current implementation might be improved and made more consistent (without designing it into a corner concerning future decisions) by allowing a closure defined in class X to follow the same rules as any other method also defined in X. Any thoughts or feedback would be very much appreciated. :-) Thanks for your time and consideration, - Nate Abele Lead Developer, CakePHP