This article mainly introduces the use of closure class in PHP, has a certain reference value, and now share to everyone, the need for friends can refer to
The closure class is also called anonymous function, which is introduced in php5.3. As the name implies, anonymous functions are functions that do not have names defined. This article introduces you to the use of closure class in PHP and the details, the need for friends can refer
Closure, an 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 a very memorable understanding of the definition of anonymous functions.
The Closure class (PHP 5 >= 5.3.0) is an introduction to classes that represent anonymous functions. Anonymous functions (introduced in PHP 5.3) produce this type of object, let's take a look at the use and introduction of the PHP closure class.
The PHP closure class was previously introduced in the PHP pre-defined 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-Constructors for prohibiting instantiation
closure::bind-copy a closure that binds the specified $this object and class scope.
closure::bindto-Copy the current closure object, binding the specified $this object and the class scope.
See an example of binding $this objects and scopes:
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 the class into a closure:
Class Invoker {public function __invoke () {return __method__;}} $obj = new Invoker;echo $obj (); Invoker::__invoke
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!