Function: php53 New closure syntax introduction functionuse {}: reprinted original Post address: blog. csdn. netlgg201articledetails60000564n an anonymous function is directly defined for transfer. in previous versions, this is unavailable. now, this syntax is very comfortable to reprint the original post address: http://blog.csdn.net/lgg201/article/details/6127564
/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. */functioncounter () {$ counter = 1; returnfunction () 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 ";?>
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above introduces function use {} in the closure syntax added by php 53, including function content, and hopes to help friends who are interested in PHP tutorials.