This will not work.

As stated in the PHP documentation "Static closures cannot have any bound
object "

A static Closure has no context of "this" just as with any other static
object.

A workaround is to pass in the Closure as a parameter to achieve a similar
result.

class TestClass {
    public static function testMethod($function) {
        $testInstance = new TestClass();
        $function = $function->bindTo($testInstance);
        call_user_func_array($function, [$testInstance]);
        // should be true
    }
}

TestClass::testMethod(function($param){
    var_dump($this === $param);
});


On Fri, May 31, 2013 at 2:32 PM, Nathaniel Higgins <n...@nath.is> wrote:

> I'm talking about PHP 5.4. `bindTo` is a Closure method in PHP 5.4, and
> allows you to set the `$this` variable inside of a Closure. However,
> apparently you can't use it on Closures created inside static methods.
>
> I knew that you could create another function which would return the
> Closure, however, that isn't what I asked. I wanted to either use the
> `bindTo` method on a static Closure, or create a non-static Closure in a
> static method.
>
>
> On 31 May 2013 19:25, David Harkness <davi...@highgearmedia.com> wrote:
>
> > On Fri, May 31, 2013 at 10:54 AM, Nathaniel Higgins <n...@nath.is> wrote:
> >
> >> Is it possible to bind an instance to a static closure, or to create a
> >> non-static closure inside of a static class method?
> >>
> >
> > PHP doesn't have a method to do this. In JavaScript you can use jQuery's
> >
> >     var func = $.proxy(function () { ... }, object);
> >
> > In fact, you cannot use $this inside a closure at all (unless 5.4 has
> > added a way that I haven't seen yet). You can get around that by
> declaring
> > a local variable to hold a reference to the instance to use with "use".
> It
> > looks strange here because you're also passing in $testInstance for the
> > comparison.
> >
> >     <?php
> >     class TestClass {
> >         public static function testMethod() {
> >             $testInstance = new TestClass();
> >             $closure = $testInstance->createClosure($testInstance);
> >
> >             call_user_func($closure);
> >             // should be true
> >         }
> >
> >         private function createClosure($testInstance) {
> >             $self = $this;
> >             return function() use ($self, $testInstance) {
> >                 return $self === $testInstance;
> >             }
> >         }
> >     }
> >
> >     TestClass::testMethod();
> >
> > Peace,
> > David
> >
> >
>
>
> --
> Thanks,
> http://nath.is
> @NatIsGleek <http://twitter.com/natisgleek>
> -- --
> Unless otherwise specified, this conversation can be classed as
> confidential, and you have no permission to share all, part, or even the
> gist of this conversation with anyone not part of the original
> conversation. If in doubt, please contact be with the above links, or on my
> UK phone number - *07427558947*. Please be thoughtful of what time it is in
> the UK at your time of calling.
>



-- 
Nickolas Whiting
Lead Developer
X Studios
321-281-1708x107

Reply via email to