<? Php /** * The Code mentioned below is run in PHP5.3 or later. */ Function callback ($ callback ){ $ Callback (); } // Output: This is a anonymous function. <br/>/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. <br/>/n "; }); // 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... // 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. <br/>/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 // 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. <br/>/n "; }; $ Msg = "Hello, everybody "; Callback ($ callback ); // Output: This is a closure use object, msg is: Hello, everyone. <br/>/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}. <br/>/n "; }; $ Obj = (object) "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. $ 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 lazy bind, msg is: Hello, everybody. <br/>/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}. <br/>/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 (). "<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 "; ?> |