[PHP]View PlainCopy
- <?php
- /**
- * AUTHOR:SELFIMPR
- * Mail: [email protected]
- * blog:http://blog.csdn.net/lgg201
- * The code mentioned below runs through the 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 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.<br/>/n";
- });
- Output: This was a closure use string value, msg Is:hello, Everyone.<br/>/n
- Here is the first definition of a closure, this time the hukou has a name ...
- Use, a fresh guy ...
- As we all know, 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 then 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 by the underlying type parameter passed as the value of the pass-through method.
- $msg = "Hello, everyone";
- $callback = function () use ($msg) {
- Print "This is a closure with string value, MSG is: $msg. <br/>/n";
- };
- $msg = "Hello, everybody";
- Callback ($callback);
- Output: This was a closure use string value lazy bind, msg is:hello, Everybody.<br/>/n
- In other ways, we use the reference
- You can see that this output is the value after the closure definition ...
- This is not difficult to understand, we use as a reference, the closure using the $MSG variable address
- When the value of this address is changed after facing $msg, the value of this address is changed naturally in the closure.
- $msg = "Hello, everyone";
- $callback = function () use (&$msg) {
- Print "This was a closure use string value lazy Bind, MSG is: $msg. <br/>/n";
- };
- $msg = "Hello, everybody";
- Callback ($callback);
- Output: This was a closure use object, msg Is:hello, Everyone.<br/>/n
- The output in the closure is the previously copied value of Hello, the object of everyone, followed by a re-assignment of the name $obj.
- You can think about it.
- 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}. <br/>/n ";
- };
- $obj = (object) "Hello, everybody";
- Callback ($callback);
- Output: This was a closure use object, msg Is:hello, Everybody.<br/>/n
- or follow the steps above and step it through:
- 1. obj name points to Hello, everyone object
- 2. The closure produces a reference to the Hello, the Everyone object
- 3. Modifies the scalar value of the object that the obj name points to (that is, the Hello, everyone object)
- 4. 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}. <br/>/n ";
- };
- $obj->scalar = "Hello, everybody";
- Callback ($callback);
- Output: This was a closure use object lazy bind, msg is:hello, Everybody.<br/>/n
- What does a closure refer to? & $obj, the quote from the closure points to the address pointed to by the name $obj.
- 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}. <br/>/n ";
- };
- $obj = (object) "Hello, everybody";
- Callback ($callback);
- /**
- * A counter generator that uses closures
- * This is actually a reference to Python in the introduction of closures when the example ...
- * We can consider this:
- * 1. Each time the counter function is called, a local variable $counter is created, initialized to 1.
- * 2. Then a closure is created, and the closure produces a reference to the local variable $counter.
- * 3. The function counter returns the created closure and destroys the local variable, but at this point there is a reference to the $counter by the closure.
- * It's not going to be recycled, so we can understand that the closure, returned by the function counter, carries 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.
- */
- function counter () {
- $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 () {}