PHP, class Closure

I’m trying to understand about the closure class, the manual is, Everything is in the manual Link ,

Closing :: __ The construct is a constructor that prohibits instantiation. If I understand correctly, the only instance of this class is to assign an anonymous function variable.

But I did not understand a few lines:

Closure :: bind . Duplicates a closure with a specific related object and class scope.

Closure :: bindTo . Duplicates a closure using a new related object and scope.

And the last in the guide, I did not understand this sentence:

Besides the methods listed here, this class also has an __invoke method. This is to match other classes that implement calling magic, since this method is not used to call a function.

If someone tries to explain to me what it is, I will be very grateful, Good day.

+5
source share
2 answers

This applies to defiant magic .

As I understand it, for any class that contains a method __invoke, its instances can be called as if it were a function. Closure::__invokeacts like that.

i.e. when it $foohas a class Closure(anonymous function), the call $foo($bar)will call $foo->__invoke(bar)(although the member __invokeshould not be called directly, this is just to show how it works).

, :

$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

$greet Closure. $greet->__invoke - function($name){ printf("Hello %s\r\n", $name); }

, Closure::__invoke Magic Method.

+7

, bind bindTo , :

$cl->bindTo($obj)

Closure::bind($cl, $obj)

__invoke, , , . , (, ) , . , , , .

+3

All Articles