How to use PHP closure class and its introduction

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.