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


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to