PHP: Anonymous function (closure) [i]

Source: Internet
Author: User

Excerpt from: http://www.cnblogs.com/starlion/p/3894578.html

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 ($strecho$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 () {    $funcfunction() {        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 // function returns 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.

functionClosureFunc4 () {$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?

functionClosureFunc5 () {$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

functionClosureFunc5 () {$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

Reference:

Http://www.cnblogs.com/yjf512/archive/2012/10/29/2744702.html why to introduce closures

http://blog.csdn.net/lgg201/article/details/6127564 php 5.3.0 function () use () {}

PHP: Anonymous function (closure) [i]

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.