I am trying to understand about the closure class, at the manual there is,
All at the manual Link,
Closure::__construct – Constructor that disallows instantiation. If i understand it right the only instance of this class is for anonymous function variable assignment.
But i did not understand few lines:
Closure::bind — Duplicates a closure with a specific bound object and class scope.
Closure::bindTo — Duplicates the closure with a new bound object and class scope.
And the last at the manual i did not understand this sentence:
Besides the methods listed here, this class also has an __invoke
method. This is for consistency with other classes that implement
calling magic, as this method is not used for calling the function.
If can some one please try to explain thoes lines to me i will be very thankful, Have a nice day.
It is referring to the calling magic.
As my understanding, for any class that contains the method
__invokewhich its instances can be called as if it is a function. TheClosure::__invokeacts like that.i.e. when
$foois of classClosure(anonymous function), calling$foo($bar)will call$foo->__invoke(bar)(although the__invokemember is not meant to be called directly, this is just to show how it works).When you define anonymous functions, you do this:
Now,
$greetis of classClosure. and$greet->__invokeis sort of equal tofunction($name){ printf("Hello %s\r\n", $name); }And remember,
Closure::__invokeis a Magic Method.