How to Use Closure in PHP

Source: Internet
Author: User
The Closure class is also called an anonymous function and introduced in php5.3. As the name implies, an anonymous function is a function with no name defined. This article introduces how to use and explain the Closure class in php.

PHP Closure class usage and detailed explanation, phpclosure

Closure, an Anonymous function, also known as Anonymous functions, was introduced at php5.3. An anonymous function is a function without a defined name. Remember this to understand the definition of anonymous functions.

Closure class (PHP 5> = 5.3.0) describes classes used to represent anonymous functions. anonymous functions (introduced in PHP 5.3) will generate this type of object. Next let's take a look at the usage and introduction of the PHP Closure class.

PHP Closure class was previously introduced in PHP pre-defined interface, but it is not interface, it is an internal final class. The Closure class is used to represent anonymous functions. All anonymous functions are Closure class instances.

$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 disable instantiation
Closure: bind-copy a Closure and bind it to the specified $ this object and Class scope.
Closure: bindTo-copy the current Closure object and bind it to the specified $ this object and Class scope.

Let's look at an example of binding $ 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 convert the class into a closure:

class Invoker {  public function __invoke() {return __METHOD__;}}$obj = new Invoker;echo $obj(); //Invoker::__invoke

The above content is the usage and explanation of the Closure class in PHP, which I hope everyone will like.

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.