Re: Closures and $this

From: Date: Wed, 18 Nov 2009 21:23:55 +0000
Subject: Re: Closures and $this
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-46091@lists.php.net to get a copy of this message
Hello,

On Wed, Nov 18, 2009 at 11:44 AM, Stanislav Malyshev <stas@zend.com> wrote:
> I have very hard time imagining somebody that would be comfortable using
> such a construct. What happened to keeping it simple? And why every tiniest
> feature of every remotest imaginable use case has to be brought into the
> language syntax?

That syntax made my eyes hurt, but I think really the problem is that
people want to dynamically add methods with $this in the closure
pointing the the object it has been attached too. Really I like A. but
can see cases for rebinding (C). Maybe a magic method like
__attach(string methodName, function method) that would rebind $this
in closures would be a solution. But it would be the first magic
method I can think of that isn't overridden by the defining class.

$o = new StdClass;
$o->bar = 'bar';
$o->__attach('foo', function() { echo $this->bar; });
$o->foo(); // bar

Just a thought.

-Chris


Thread (36 messages)

« previous php.internals (#46091) next »