This article mainly introduces the use of PHP closure class, interested in the reference of friends, I hope to be helpful to everyone.
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
Summary : The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Php+mysql+jquery statistics current number of users online
PHP implementation of digital format, the number of every three comma-plus function function
PHP Chinese Vertical Conversion Program