Closure, anonymous function, also known as anonymous functions, was introduced when php5.3. anonymous functions are functions that do not have a name defined . This is an understanding of the definition of anonymous functions.
Closure Class (PHP 5 >= 5.3.0) introduces a class that is used to represent anonymous functions. Anonymous functions (introduced in PHP 5.3) will produce this type of object, let's take a look at the PHP closure class using methods and introduction.
PHP Closure class was previously introduced in the PHP predefined interface, but it is not interface Oh, it is an internal final class. The closure class is used to represent anonymous functions, and all anonymous functions are instances of the closure class.
$func = function () {
echo ' func called ';
};
Var_dump ($func); Class Closure#1 (0) {}
$reflect =new reflectionclass (' Closure ');
Var_dump (
$reflect->isinterface (),//false
$reflect->isfinal (),//true
$reflect->isinternal ()//true
);
The closure class structure is as follows:
Closure::__construct-constructor used to prevent instantiation
closure::bind-copies a closure that binds the specified $this object and class scope.
closure::bindto-duplicates the current closure object, binding the specified $this object and class scope.
Look at an example of a bound $this object and scope:
Class Lang
{
private $name = ' php ';
}
$closure = function () {return
$this->name;
};
$bind _closure = Closure::bind ($closure, New Lang (), ' Lang ');
echo $bind _closure (); Php
In addition, PHP uses the Magic Method __invoke () to turn a class into a closure:
Class Invoker {public
function __invoke () {return __method__;}
}
$obj = new Invoker;
Echo $obj (); Invoker::__invoke
The above content is small to share the PHP in the closure class of use methods and detailed, I hope you like.