Reprint Original Address: http://blog.csdn.net/lgg201/article/details/6127564
functioncallback($callback) {$callback();}//output: This is a anonymous function.
/n//Here is a direct definition of an anonymous function to pass, which is not available in previous versions.//Now, this syntax is very comfortable, and JavaScript syntax basically consistent, the reason is basically, need to continue to look down//Conclusion: a comfortable grammar is bound to be popular.Callback function() {Print"This is a anonymous function.
/n ";});//output: This was a closure use string value, msg Is:hello, everyone.
/n//Here first defines a closure, this time the hukou has a name ...//use, a fresh guy ...//Well-known, closures: Intrinsic functions use variables defined in external functions.//In PHP's new open closure syntax, we are using use to define variables outside the closure.//Here we use the external variable $msg, after the definition, and the value of the change, the closure is executed after the output is the original value//Conclusion: The value of the closure use is determined when the closure is created with the underlying type parameter passed as a pass-through value.$msg="Hello, everyone.";$callback= function()use($msg) {Print"This was 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//In a different way, we use the reference//You can see that this output is the value after the closure definition ...//This is actually not difficult to understand, we use by reference, that closure using the $MSG variable address//When the value of this address is changed after facing the $msg, the value of the location in the closure is changed naturally.$msg="Hello, everyone.";$callback= function()use(&$msg) {Print"This was a closure use string value lazy Bind, MSG is: $msg.
/n ";};$msg="Hello, everybody."; Callback ($callback);//output: This was a closure use object, MSG Is:hello, everyone.
/nThe output in the //closure is the previously copied value of Hello, the object of everyone, followed by a re-assignment of the name $obj.//Can be considered as such//1. obj is the object hello, everyone's name//2. Object Hello, everyone is closed use, the closure produces a reference to the Hello, everyone object//3. obj is modified to Hello, everybody the name of the object//4. Note that the entity whose name obj represents has changed, not the Hello, the Everyone object, the output of the natural closure or the previous Hello, everyone$obj= (object)"Hello, everyone.";$callback= function()use($obj) {Print"This is a closure with object, MSG is: {$obj->scalar}.
/n ";};$obj= (object)"Hello, everybody."; Callback ($callback);//output: This was a closure use object, msg Is:hello, everybody.
/n//or follow the steps above to step it up://1. obj name points to Hello, everyone object//2. Closures produce a reference to the Hello, everyone object//3. Modifying the scalar value of the object that the obj name points to (that is, the Hello, everyone object)//4. Execute closures, the output is naturally hello, everybody, because there is actually only one real object$obj= (object)"Hello, everyone.";$callback= function()use($obj) {Print"This is a closure with object, MSG is: {$obj->scalar}.
/n ";};$obj->scalar ="Hello, everybody."; Callback ($callback);//output: This was a closure use object lazy bind, msg is:hello, everybody.
/n//What is the closure reference? & $obj, the closure produces a reference to the address pointed to by the name $obj.//So, no matter how obj changes, it is not to escape ....//So, the output is the changed value$obj= (object)"Hello, everyone.";$callback= function()use(&$obj) {Print"This is a closure with object lazy bind, MSG is: {$obj->scalar}.
/n ";};$obj= (object)"Hello, everybody."; Callback ($callback);/** * A counter generator using closures * Here is a reference to Python in the case of closures ... * We can consider this: * 1. Each time the counter function is called, a local variable $counter is created, initialized to 1. * 2. Then create a closure, and the closure generates a reference to the local variable $counter. * 3. The function counter returns the created closure and destroys the local variable, but at this time there is a $counter reference to the closure, * It is not recycled, so we can understand that the closure returned by the function counter, carrying a free state of the * variable . * 4. Since each call to counter creates separate $counter and closures, the returned closures are independent of each other. * 5. Executes the returned closure, and the Free State variable it carries is self-incremented and returned, resulting in a counter. * Conclusion: This function can be used to generate counters that are independent of each other. */ functioncounter() {$counter=1;return function()use(&$counter) {return$counter++;};}$counter 1= Counter ();$counter 2= Counter ();Echo"Counter1:".$counter 1() ."
/n ";Echo"Counter1:".$counter 1() ."
/n ";Echo"Counter1:".$counter 1() ."
/n ";Echo"Counter1:".$counter 1() ."
/n ";Echo"Counter2:".$counter 2() ."
/n ";Echo"Counter2:".$counter 2() ."
/n ";Echo"Counter2:".$counter 2() ."
/n ";Echo"Counter2:".$counter 2() ."
/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); }); });
The above introduces the new closure syntax of PHP 53 introduces the function use {}, including the function of the content, I hope that the PHP tutorial interested in a friend helpful.