/** * The Code mentioned below is run in PHP5.3 or later. */ Function callback ($ callback ){ $ Callback (); } // Output: This is a anonymous function. /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 why it is basic needs to be further viewed. // Conclusion: a comfortable syntax is welcome. 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. the account name is included in this account... // 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, 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 // You can find that the output is the value defined by the closure... // 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 value of this address is output in the closure. $ 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 with the previously copied value "Hello, everyone", followed by a new value for the $ obj name. // You can consider this // 1. obj is the name of the object Hello, everyone // 2. the object Hello and everyone is 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 represented by the name obj has changed, rather than the Hello, everyone object. the output of the natural closure is the preceding Hello, everyone object. $ 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 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. $ 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 closures * The reference here is an 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. Therefore, we can understand that the closure returned by the counter function carries a free state * Variable. * 4. since each 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. */ 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 "; ?> |