My opinion is, that we simply have to change PHP closures' scoping to be
more similar with JavaScript functions' scope. That would solve the current
challenge pointed on by @jpauli, thus closure would have access to private
object's members only if it was defined in that object's class / method
definition, so then engine can know that it was programmers intention. I
mean something like this:

<?php

$p = function() { $this->privateMember = new stdClass; };

class C {
    private $privateMember;
    private $closure;
    function __construct() {
        $this->closure = function() {
            $this->privateMember = 32;
        };
    }
    function callClosure() {
        $this->closure->call($this);
    }
}

$c = new C;
$c->callClosure(); // would succeed
$p->call($c); // Fatal error

?>

What do you think about this kind of behavior?


Best regards,
Kubo2


2015-04-17 22:03 GMT+02:00 Marco Pivetta <ocram...@gmail.com>:

> On 17 April 2015 at 18:43, Ferenc Kovacs <tyr...@gmail.com> wrote:
>
> > For the record this isn't exclusive to Closure::call, afaik the same
> thing
> > is possible with Closure::bind() since 5.4
> > http://3v4l.org/hlFS4
> > And there are/were a decent amount of discussion (and AFAIR even hydrate
> > libraries using this trick) about this, for example:
> >
> >
> http://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
> >
>
> Also note that this is the only way to access private properties by-ref
> (yes, I do need that for my horrible use-cases. No, don't ask about it.)
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/
>

Reply via email to