Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a PHP static anonymous function really work?

I'm trying to learn PHP, and now I'm stuck in 'static anonymous function'.

I found this in a tutorial (http://www.slideshare.net/melechi/php-53-part-2-lambda-functions-closures-presentation)

"Object Orientation

  • Lambda Functions are Closures because they automatically get bound to the scope of the class that they are created in.
  • '$this' is not always needed in the scope.
  • Removing '$this' can save on memory.
  • You can block this behaviour by declaring the Lambda Function as static."

What is wrong with this code?

I get this error:

Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in C:\wamp\www\z-final\a.php on line 11

Why this code line doesn't work "return static function(){var_dump($this);};" ?

class foo
{
    public function getLambda()
    {
        return function(){var_dump($this);};
    }

    public function getStaticLambda()
    {
        return static function(){var_dump($this);};
    }
}

$foo = new foo();
$lambda = $foo->getLambda();
$staticLambda = $foo->getStaticLambda();
$lambda();
$staticLambda();
like image 212
dudeson Avatar asked Sep 25 '12 09:09

dudeson


People also ask

Does PHP have anonymous functions?

Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class. printf("Hello %s\r\n", $name);

What is the point of anonymous functions?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.

Which is are true about anonymous functions?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.

What is anonymous function explain it with any suitable example?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.


1 Answers

Yes, that is perfectly valid syntax in 5.4+.

Basically, it prevents auto-binding of the current class to the closure (in fact, it prevents all binding, but more on that later).

class Foo {
    public function bar() {
        return static function() { var_dump($this); };
    }
    public function baz() {
        return function() { var_dump($this); };
    }
}

If we instantiate that on 5.4+, the closure bar() returns will have $this set to null. Just as if you made a static call to it. But baz() would have $this set to the foo instance you called baz() on.

So:

$bar = $f->bar();

$bar();

Results in:

Notice: Undefined variable: this in /in/Bpd3d on line 5

NULL

And

$baz = $f->baz();

$baz();

Results in

object(Foo)#1 (0) {

}

Make sense? Great.

Now, what happens if we take closures defined outside of a function:

$a = function() { var_dump($this); };
$a();

We get null (and a notice)

$c = $a->bindTo(new StdClass());
$c();

We get StdClass, just as you'd expect

$b = static function() { var_dump($this); };
$b();

We get null (and a notice)

$d = $b->bindTo(new StdClass());
$d();

This is where things get interesting. Now, we get a warning, a notice, and null:

Warning: Cannot bind an instance to a static closure in /in/h63iF on line 12

Notice: Undefined variable: this in /in/h63iF on line 9

NULL

So in 5.4+, you can declare a static closure, which results in it never getting $this bound to it, nor can you ever bind an object to it...

like image 137
ircmaxell Avatar answered Oct 29 '22 09:10

ircmaxell