PHP 5.3 Closure Syntax introduction function () use () {}_php tutorial

Source: Internet
Author: User
Tags closure definition php language scalar
PHP 5.3 Joins the closure syntax, which is the anonymous function, which allows developers to declare inline functions and save them in variables. While this syntax is a bit weird compared to JavaScript closures, it's a good addition to the PHP language.
The code is as follows Copy Code
/**
* The code mentioned below runs through the PHP5.3 version above.
*/
function callback ($callback) {
$callback ();
}
Output: This is a anonymous function.
/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.
/n ";
});
Output: This was a closure use string value, msg Is:hello, everyone.
/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 was a closure use string value, MSG is: $msg.
/n ";
};
$msg = "Hello, everybody";
Callback ($callback);
Output: This was a closure use string value lazy bind, msg is:hello, everybody.
/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.
/n ";
};
$msg = "Hello, everybody";
Callback ($callback);
Output: This was a closure use object, MSG Is:hello, everyone.
/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}.
/n ";
};
$obj = (object) "Hello, everybody";
Callback ($callback);
Output: This was a closure use object, msg Is:hello, everybody.
/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}.
/n ";
};
$obj->scalar = "Hello, everybody";
Callback ($callback);
Output: This was a closure use object lazy bind, msg is:hello, everybody.
/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}.
/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 (). "
/n ";
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
?>

http://www.bkjia.com/PHPjc/628696.html www.bkjia.com true http://www.bkjia.com/PHPjc/628696.html techarticle PHP 5.3 Joins the closure syntax, which is the anonymous function, which allows developers to declare inline functions and save them in variables. Although this syntax is a little bit weird compared to JavaScript closures, it 's ...

  • 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.