anonymous function and Closure (closure) usage in PHP

Source: Internet
Author: User
This article mainly introduces the anonymous function and the closure (closure) usage in PHP, and I hope it will help you with your friends ' reference.

One: anonymous function ( can be used at php5.3.0 or above )

The anonymous function in PHP (Anonymous functions), also called the closure function (closures), allows you to specify a function without a name. The most common is the parameter value of the callback function. (http://php.net/manual/zh/functions.anonymous.php)

Definition of anonymous function:

$closureFunc = function () {...};

Eg: assigning an anonymous function to a variable and invoking it through a variable



$closureFunc = function ($str) {echo $str;  };  $closureFunc ("Hello world!");


Output: Hello world!

Two: Closures

2.1 The anonymous function in the normal function, or the anonymous function can be returned, which constitutes a simple closure



function ClosureFunc1 () {    $func = function () {        echo "Hello";    };    $func ();} CLOSUREFUNC1 ();//output: Hello


2.2 referencing local variables in anonymous functions



function ClosureFunc2 () {    $num = 1;    $func = function () {        echo $num;    };    $func ();} ClosureFunc2 ();//notice:undefined Variable:num


After the above function runs, it will report notice error, indicating that we can not use the local variables in the anonymous function, it is necessary to refer to a PHP keyword used, the code is as follows



function ClosureFunc2 () {    $num = 1;    $func = function () use ($num) {        echo $num;    };    $func ();} CLOSUREFUNC2 ();//output: 1


2.3 Returning anonymous functions



function closureFunc3 () {    $num = 1;    $func = function () use ($num) {        echo $num;    };    return $func;} $func = ClosureFunc3 (); function returns the anonymous function $func (); Then we are using $func ()//output: 1


2.4 How do we pass an anonymous function to an anonymous function when we return it? In fact, as with normal function arguments.



function ClosureFunc4 () {    $num = 1;    $func = function ($str) use ($num) {        echo $num;        echo "\ n";        echo $str;    };    return $func;} $func = ClosureFunc4 (); $func ("Hello, Closure4");//output://1//hello, Closure4


2.5 How do I use closures to change the value of a context-referenced variable?



function ClosureFunc5 () {    $num = 1;    $func = function () use ($num) {        echo ' \ n ';        $num + +;        echo $num;    };    echo "\ n";    echo $num;    return $func;} $func = ClosureFunc5 (); $func (); $func (); $func ();//output://1//2//2//2

Look at the above input results, obviously did not achieve the purpose, in fact, just add a & quote symbol can be



function ClosureFunc5 () {    $num = 2;    $func = function () use (& $num) {        echo "\ n";        $num + +;        echo $num;    };    echo "\ n";    echo $num;    return $func;} $func = ClosureFunc5 (); $func (); $func (); $func ();//output://2//3//4//5


2.6 Passing anonymous functions as parameters



function Callfunc ($func) {    $func ("argv");} Callfunc (function ($str) {    echo $str;}) Output://argv

Related recommendations:

Knowledge about JavaScript closures and how to use them

JS Closure use detailed

A deep understanding of the Python closure mechanism

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.