Re: Anonymous functions inside static methods.

From: Date: Thu, 08 May 2014 22:22:14 +0000
Subject: Re: Anonymous functions inside static methods.
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi

> On 9 May, 2014, at 6:02 am, Levi Morrison <[email protected]> wrote:
> 
> See this 3v4l (contents at bottom of the message): http://3v4l.org/pHkf7
> 
> I think Bar::bar() should definitely return 'Bar' (currently returns 'Foo')
> but what about the Bar::Baz() call?
> 
> Until today I didn't even know you could declare a static anonymous
> function, so I'm not really sure what the expected behavior is.
> 
> Script contents:
> <?php
> 
> class Foo {
> static function bar() {
>        $foo = static function() {
>            return get_called_class();
>        };

From what I gather, the use of static on closures only limits as to what you can bind it to, i.e.
->bindTo() will not accept an instance to bind to.

> 
>        return $foo();
>    }
> static function baz() {
>        $foo = function() {
>            return get_called_class();
>        };
> 
>        return $foo();
>    }
> }
> 
> class Bar extends Foo {
> 
> }
> 
> 
> assert(Bar::bar() === 'Bar');
> assert(Bar::baz() === 'Bar');


Thread (9 messages)

« previous php.internals (#74073) next »