PHP closure function Closures anonymous function

Source: Internet
Author: User

/* *  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

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.