/* * anonymous function * closure function (closures) * dependent on anonymous function closure * child functions can use local variables in the parent Function. This behavior is called closure * connection closure and external variables keywords use * closure Two features * 1. as a reference to a function variable, when the function returns, it is Active. * 2. a closure is when a function returns, a stack that does not release Resources. When the * closure function returns, The function internal variable is active, and the function remains in the stack Area. * * Summary * 1. The outer layer of the closure is a function * 2. there are function * 3 inside the Closure. the closure will return the intrinsic function * 4. the function returned by the closure cannot have a return (or it really ends) * 5. after the closure is executed, the inner variable of the closure is present, and the inner variable of the inner function of the closure does not exist. Application scenarios for * * closures * 1. the security of the variables within the protection function, the variables in the outer function can only be accessed by internal functions, and cannot be accessed by other means, thus protecting the security of variables in the outer function. &NBSP;*&NBSP;2. maintains a variable in memory. closure, returns an intrinsic function, uses the variables in the outer function, so the variables in the outer function remain in memory. therefore, each execution will be used. * */ $var = function ($a, $b) { echo ' I am anonymous function <br/> '; return $a + $b; };//anonymous function must be added with a semicolon echo $var ( Var_dump ($var);//object (Closure) [1] Object An instance of the built-in class Closure that is, the object Function oNe () { echo ' 1<br/> '; function two () { echo ' 2<br/> '; } Function three () { echo ' 3<br/> '; } two ();} One (); function demo () { $a = 10; $b = 20 ; $one = function ($param) use (& $a, $b) {//using external variables with use $a, $b Use & Reference Change the value of $ A echo $param. " <br/> " ; $a ++; echo $a. " <br/> "; echo $b." <br/> "; }; // $one (' hello world '); // echo $a; return $one;} $var = demo (); $var (' yangsir ');//$a variable is not released 11$var (' wangsir ');//$a variable is not released 12function test ($fun) { echo $fun ();} Test (function () { return ' I am an anonymous function to transmit parameters ';});
This article is from the "jin sha harbor" blog, Please be sure to keep this source http://11410485.blog.51cto.com/11400485/1840249
PHP closure function Closures anonymous function