Function () use (){}

Source: Internet
Author: User
Tags closure definition
& Lt ;? Php/*** the code mentioned below is run in PHP5.3 or later. */functioncallback ($ callback) {$ callback ();} // output: Thisisaanonymousfunction. & lt; br/& gt;/n // An anonymous letter is directly defined here.
 /N // An anonymous function is directly defined here for transfer. in previous versions, this is unavailable. // at present, this syntax is very comfortable. it is basically the same as the javascript syntax. the reason for this is the basics, you need to continue to look down. // conclusion: a comfortable syntax will certainly be popular. callback (function () {print "This is a anonymous function.
/N ";}); // output: This is a closure use string value, msg is: Hello, everyone.
/N // a closure is defined first here. this account has a name on it... // use, a fresh guy... // well known, closure: internal functions use variables defined in external functions. // In The New closure syntax of PHP, we use to use the variables defined externally by the closure. // Here we use the external variable $ msg. after the definition is complete, the value is changed. after the closure is executed, the original value is output. // conclusion: for the basic type parameter passed by passing values, the value of the closure use is determined when the closure is created. $ msg = "Hello, everyone"; $ callback = function () use ($ msg) {print "This is a closure use string value, msg is: $ msg.
/N ";}; $ msg =" Hello, everybody "; callback ($ callback); // output: This is a closure use string value lazy bind, msg is: Hello, everybody.
/N // for another reference method, we use the reference method to use // we can find that this output is the value after the closure definition... // This is not hard to understand. We use it as a reference, the closure uses the address of the $ msg variable. // when the value on the $ msg address is changed, the closure outputs the value of this address, naturally changed. $ msg = "Hello, everyone"; $ callback = function () use (& $ msg) {print "This is a closure use string value lazy bind, msg is: $ msg.
/N ";}; $ msg =" Hello, everybody "; callback ($ callback); // output: This is a closure use object, msg is: Hello, everyone.
/N // The closure outputs the object whose previously copied value is Hello and everyone, followed by a new value for the $ obj name. // you can consider it like this // 1. obj is the name of the object Hello, everyone // 2. the object Hello and everyone is used by the closure. the closure generates a reference to the Hello and everyone objects. // 3. obj is changed to Hello, everybody, and the object name // 4. note that the object name obj indicates that the object has changed, rather than the Hello, everyone object. the output of the natural closure is the Hello, everyone $ obj = (object) "Hello, everyone "; $ callback = function () use ($ obj) {print "This is a closure use object, msg is: {$ obj-> scalar }.
/N ";}; $ obj = (object)" Hello, everybody "; callback ($ callback); // output: This is a closure use object, msg is: Hello, everybody.
/N // follow the steps above to step by step: // 1. obj name points to Hello, everyone object // 2. the closure generates a reference pointing to the Hello, everyone object // 3. modify the scalar value of the object (that is, the Hello, everyone object) pointed to by obj name. // 4. execute the closure, and the output is naturally Hello and everybody, because there is actually only one real object $ obj = (object) "Hello, everyone"; $ callback = function () use ($ obj) {print "This is a closure use object, msg is: {$ obj-> scalar }.
/N ";}; $ obj-> scalar =" Hello, everybody "; callback ($ callback); // output: This is a closure use object lazy bind, msg is: hello, everybody.
/N // What does the closure reference? & $ Obj: the reference generated by the closure points to the address pointed to by the $ obj name. // therefore, no matter how the obj changes, it cannot be escaped .... // therefore, the output is the changed value $ obj = (object) "Hello, everyone"; $ callback = function () use (& $ obj) {print "This is a closure use object lazy bind, msg is: {$ obj-> scalar }.
/N ";}; $ obj = (object)" Hello, everybody "; callback ($ callback ); /*** a counter generator using the closure ** here we actually use the example of introducing the closure in python... * we can consider this as follows: * 1. each time the counter function is called, a local variable $ counter is created and initialized to 1.*2. create a closure that generates a reference to the local variable $ counter. * 3. the counter function returns the created closure and destroys local variables. However, a closure references $ counter at this time. * It is not recycled. Therefore, we can understand it as follows, the closure returned by the counter function carries a free * variable. * 4. since every counter call creates an independent $ counter and closure, the returned closure is independent of each other. * 5. execute the returned closure to automatically add and return the Free State variables carried by the returned closure. The result is a counter. * conclusion: This function can be used to generate mutually independent counters. */function counter () {$ counter = 1; return function () use (& $ counter) {return $ counter ++ ;}}$ counter1 = counter (); $ counter2 = counter (); echo "counter1 :". $ counter1 ()."
/N "; echo" counter1: ". $ counter1 ()."
/N "; echo" counter1: ". $ counter1 ()."
/N "; echo" counter1: ". $ counter1 ()."
/N "; echo" counter2: ". $ counter2 ()."
/N "; echo" counter2: ". $ counter2 ()."
/N "; echo" counter2: ". $ counter2 ()."
/N "; echo" counter2: ". $ counter2 ()."
/N ";?>

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.