PHP 5.3 New Closure Syntax introduction function () use () {}

Source: Internet
Author: User
Tags closure definition scalar

Original Address http://bbs.csdn.net/topics/360002529

<?PHP/** * author:selfimpr * mail: [email protected] * blog:http://blog.csdn.net/lgg201 * The code mentioned below is running through PHP5.3 version above.*/ function Callback($callback) {    $callback();} //output: This is a anonymous function.<br/>\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 Grammar basically consistent, the reason is basic, need to continue to look down//conclusion: a comfortable grammar is bound to be popular.Callback(function() {    Print"This is a anonymous function.<br/>\n";}); //output: This was a closure use string value, msg Is:hello, everyone.<br/>\n//here first defines a closure, this time the hukou has a name ...//use, a fresh Guys ...//well-known, closures: Intrinsic functions use variables defined in external functions.//In PHP's new open closure syntax, we use using the variables defined externally by the closure.//Here we use the external variable $msg, after the definition, and then change its value, When a closure is executed, the output is the original value//conclusion: the underlying type parameter passed as the value of the packet, and the value of the closure use is determined in the closure creation.$msg= "Hello, everyone";$callback=function() Use($msg) {    Print"This was a closure use string value, MSG is:$msg. &LT;BR/>\n ";};$msg= "Hello, everybody";Callback($callback); //output: This is a closure using string value lazy bind, msg Is:hello, Everybody.<br/>\n//in another way, we use a reference to use//can be sent Now the output is the value after the closure definition ...//This is actually not difficult to understand, we use the reference method, the closure using the $MSG variable address//After the face $msg this address value has been changed, the closure of the value of the address after the output, naturally changed.$msg= "Hello, everyone";$callback=function() Use(&$msg) {    Print"This was a closure use string value for lazy bind, MSG is:$msg. &LT;BR/>\n ";};$msg= "Hello, everybody";Callback($callback); //output: This is a closure use object, msg Is:hello, everyone.<br/>\n//the output in the closure is a previously copied value of Hello, the object of everyone, followed by the $obj this A re-assignment of a name.//The//1 can be considered in this way. obj is the object hello, and everyone's name is//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}. &LT;BR/>\n ";};$obj= (Object) "Hello, Everybody";Callback($callback); //output: This is a closure using object, MSG Is:hello, Everybody.<br/>\n//, or follow the steps above, step into it://1. obj name points to Hello, every One object//2. The closure produces a reference to the Hello, everyone object//3. Modifies the scalar value//4 of the object that the obj name points to (that is, the Hello, everyone object). Execution closure, the output of the natural is 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}. &LT;BR/>\n ";};$obj->scalar = "Hello, everybody";Callback($callback); //output: This is a closure use object lazy bind, msg is:hello, everybody.<br/>\n//What is the closure of the package? & $obj, closures produce a reference to the $ The address that the name of obj points to.//Therefore, 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}. &LT;BR/>\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. Counter function each call, create a local variable $counter, 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 * 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() . "<br/>\n";Echo"Counter1:".$counter 1() . "<br/>\n";Echo"Counter1:".$counter 1() . "<br/>\n";Echo"Counter1:".$counter 1() . "<br/>\n";Echo"Counter2:".$counter 2() . "<br/>\n";Echo"Counter2:".$counter 2() . "<br/>\n";Echo"Counter2:".$counter 2() . "<br/>\n";Echo"Counter2:".$counter 2() . "<br/>\n";?>

PHP 5.3 New Closure Syntax introduction function () use () {}

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.