Function () Use (){}

Source: Internet
Author: User
Tags closure definition
ArticleDirectory
    • Function () Use (){}

From: http://bbs.csdn.net/topics/360002529

Introduction to function () Use () {} demo1 in the closure syntax added by PHP 5.3
 
Function Callback($ Callback){$ Callback();}Callback(Function(){Print"This is a anonymous function. <br/> \ n";});

Output: This is a anonymous function. <br/> \ n
An anonymous function is directly defined for transfer. In previous versions, this is unavailable.
Now, this syntax is quite comfortable. It is basically the same as the Javascript syntax. The reason why it is basic is that you need to continue to look down.
Conclusion: a comfortable syntax is welcome.

Demo2
  function   callback  ( $ callback  ) {  $ callback   ();}   $ 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, MSG is: Hello, everyone. <br/> \ n
A closure is defined first. The account name is included in this account...
Use, a fresh guy...
As we all know, closures: 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.
The external variable $ MSG is used here. 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.

Demo3
  function   callback  ( $ callback  ) {  $ callback   ();}   $ 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 string value lazy bind, MSG is: Hello, everybody. <br/> \ n
For another reference method, we use the reference method to use
We can see 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 variable $ MSG.
After the value on the $ MSG address is changed, the value of this address is output in the closure.

Demo4
 Function   Callback ( $ Callback  ){  $ Callback  ();}  $ OBJ = ( Object ) "Hello, everyone" ;  $ Callback = Function () Use ( $ OBJ  ){  Print "This is a closure use object, MSG is :{ $ OBJ -> Scalar}. <br/> \ n" ;};  //  Var_dump ($ OBJ );  $ OBJ = ( Object ) "Hello, everybody" ;  Callback ( $ Callback );

Output: This is a closure use object, MSG is: Hello, everyone. <br/> \ n
The closure outputs the object whose copied value is hello and everyone, followed by a new value for the $ OBJ name.
You can consider this.
1. obj is the name of the object hello and everyone.
2. The object hello and everyone are used by the closure, which generates a reference to the hello and everyone objects.
3. obj is modified to the name of the object hello, everybody.
4. Note that the object name OBJ indicates has changed, rather than the hello, everyone object. The output of the natural closure is the hello, everyone object in front.

Demo5
 Function   Callback ( $ Callback  ){  $ Callback  ();}  $ OBJ = ( Object ) "Hello, everyone" ; $ Callback = Function () Use ( $ OBJ  ){  Print "This is a closure use object, MSG is :{ $ OBJ -> Scalar}. <br/> \ n" ;};  $ OBJ -> Scalar = "hello, everybody" ;  Callback ( $ Callback );

Output: This is a closure use object, MSG is: Hello, everybody. <br/> \ n
Follow the steps above to proceed step by step:
1. The OBJ name points to the 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 the OBJ name.
4. Execute the closure. The output is hello and everybody, because there is only one real object.

Demo6
 Function   Callback ( $ Callback  ){  $ Callback  ();}  $ OBJ = ( Object ) "Hello, everyone" ;  $ Callback =Function () Use (& $ OBJ  ){  Print "This is a closure use object lazy bind, MSG is :{ $ OBJ -> Scalar}. <br/> \ n" ;};  $ OBJ = ( Object ) "Hello, everybody" ;  Callback ( $ Callback );

Output: This is a closure use object lazy bind, MSG is: Hello, everybody. <br/> \ n
What does a 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 escape ....
Therefore, the output is the changed value.

Demo7
 Function  Counter (){  $ Counter = 1 ;  Return   Function () Use (& $ Counter ){ Return   $ Counter ++ ;};} $ Counter1 = Counter ();  $ Counter2 = Counter ();  Echo "Counter1 :". $ Counter1 (). "<Br/> \ n" ;  Echo "Counter1 :". $ Counter1 (). "<Br/> \ n" ;  Echo "Counter1 :". $ Counter1 (). "<Br/> \ n" ; Echo "Counter1 :". $ Counter1 (). "<Br/> \ n" ;  Echo "Counter2 :". $ Counter2 (). "<Br/> \ n" ;  Echo "Counter2 :". $ Counter2 (). "<Br/> \ n" ;  Echo "Counter2 :". $ Counter2 (). "<Br/> \ n" ;  Echo "Counter2 :". $ Counter2 (). "<Br/> \ n ";

Result:

Counter1: 1
Counter1: 2
Counter1: 3
Counter1: 4
Counter2: 1
Counter2: 2
Counter2: 3
Counter2: 4

A counter generator using closures
Here we actually use the example of introducing closures 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,
It will not be recycled, so we can understand that the closure returned by the counter function carries a free state
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 auto-increment the free state variable carried by it and return it. The result is a counter.
Conclusion: this function can be used to generate mutually independent counters.

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.