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

Source: Internet
Author: User
Tags closure definition scalar vars

[PHP]View PlainCopy
  1. <?php
  2. /**
  3. * AUTHOR:SELFIMPR
  4. * Mail: [email protected]
  5. * blog:http://blog.csdn.net/lgg201
  6. * The code mentioned below runs through the PHP5.3 version above.
  7. */
  8. function callback ($callback) {
  9. $callback ();
  10. }
  11. Output: This is a anonymous function.<br/>/n
  12. Here is a direct definition of an anonymous function to pass, which is not available in previous versions.
  13. Now, this syntax is very comfortable, and JavaScript syntax basically consistent, the reason is basically, need to continue to look down
  14. Conclusion: A comfortable grammar is bound to be popular.
  15. Callback (function () {
  16. Print "This is a anonymous function.<br/>/n";
  17. });
  18. Output: This was a closure use string value, msg Is:hello, Everyone.<br/>/n
  19. Here is the first definition of a closure, this time the hukou has a name ...
  20. Use, a fresh guy ...
  21. As we all know, closures: Intrinsic functions use variables defined in external functions.
  22. In PHP's new open closure syntax, we are using use to define variables outside the closure.
  23. 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
  24. 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.
  25. $msg = "Hello, everyone";
  26. $callback = function () use ($msg) {
  27. Print "This is a closure with string value, MSG is: $msg. <br/>/n";
  28. };
  29. $msg = "Hello, everybody";
  30. Callback ($callback);
  31. Output: This was a closure use string value lazy bind, msg is:hello, Everybody.<br/>/n
  32. In other ways, we use the reference
  33. You can see that this output is the value after the closure definition ...
  34. This is not difficult to understand, we use as a reference, the closure using the $MSG variable address
  35. When the value of this address is changed after facing $msg, the value of this address is changed naturally in the closure.
  36. $msg = "Hello, everyone";
  37. $callback = function () use (&$msg) {
  38. Print "This was a closure use string value lazy Bind, MSG is: $msg. <br/>/n";
  39. };
  40. $msg = "Hello, everybody";
  41. Callback ($callback);
  42. Output: This was a closure use object, msg Is:hello, Everyone.<br/>/n
  43. 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.
  44. You can think about it.
  45. 1. obj is the object hello, everyone's name
  46. 2. Object Hello, everyone is closed use, the closure produces a reference to the Hello, everyone object
  47. 3. obj is modified to Hello, everybody the name of the object
  48. 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
  49. $obj = (object) "Hello, everyone";
  50. $callback = function () use ($obj) {
  51. Print "This is a closure with object, MSG is: {$obj->scalar}.  <br/>/n ";
  52. };
  53. $obj = (object) "Hello, everybody";
  54. Callback ($callback);
  55. Output: This was a closure use object, msg Is:hello, Everybody.<br/>/n
  56. or follow the steps above and step it through:
  57. 1. obj name points to Hello, everyone object
  58. 2. The closure produces a reference to the Hello, the Everyone object
  59. 3. Modifies the scalar value of the object that the obj name points to (that is, the Hello, everyone object)
  60. 4. Execution closure, the output of the natural is hello, everybody, because there is actually only one real object
  61. $obj = (object) "Hello, everyone";
  62. $callback = function () use ($obj) {
  63. Print "This is a closure with object, MSG is: {$obj->scalar}.  <br/>/n ";
  64. };
  65. $obj->scalar = "Hello, everybody";
  66. Callback ($callback);
  67. Output: This was a closure use object lazy bind, msg is:hello, Everybody.<br/>/n
  68. What does a closure refer to? & $obj, the quote from the closure points to the address pointed to by the name $obj.
  69. Therefore, no matter how obj changes, it is not to escape ....
  70. So, the output is the changed value.
  71. $obj = (object) "Hello, everyone";
  72. $callback = function () use (&$obj) {
  73. Print "This is a closure with object lazy bind, MSG is: {$obj->scalar}.  <br/>/n ";
  74. };
  75. $obj = (object) "Hello, everybody";
  76. Callback ($callback);
  77. /**
  78. * A counter generator that uses closures
  79. * This is actually a reference to Python in the introduction of closures when the example ...
  80. * We can consider this:
  81. * 1. Each time the counter function is called, a local variable $counter is created, initialized to 1.
  82. * 2. Then a closure is created, and the closure produces a reference to the local variable $counter.
  83. * 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.
  84. * It's not going to be recycled, so we can understand that the closure, returned by the function counter, carries a free State
  85. Variable.
  86. * 4. Since each call to counter creates separate $counter and closures, the returned closures are independent of each other.
  87. * 5. Executes the returned closure, and the Free State variable it carries is self-incremented and returned, resulting in a counter.
  88. * Conclusion: This function can be used to generate counters that are independent of each other.
  89. */
  90. function counter () {
  91. $counter = 1;
  92. return function () use (&$counter) {return $counter + +;};
  93. }
  94. $counter 1 = counter ();
  95. $counter 2 = counter ();
  96. Echo "Counter1:". $counter 1 ().  "<br/>/n";
  97. Echo "Counter1:". $counter 1 ().  "<br/>/n";
  98. Echo "Counter1:". $counter 1 ().  "<br/>/n";
  99. Echo "Counter1:". $counter 1 ().  "<br/>/n";
  100. Echo "Counter2:". $counter 2 ().  "<br/>/n";
  101. Echo "Counter2:". $counter 2 ().  "<br/>/n";
  102. Echo "Counter2:". $counter 2 ().  "<br/>/n";
  103. Echo "Counter2:". $counter 2 ().  "<br/>/n";
  104. ?>

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.