How to use php anonymous function use?

Source: Internet
Author: User
In this document, the anonymous function closure can inherit variables from the parent scope. Any such variables should be passed in {code...} using the use language structure. What is the mechanism of use? I don't know much about it! The document provides an example of an anonymous function.

The closure can inherit variables from the parent scope. Any such variables should be passed in the use language structure.

$ Message = 'hello'; // No "use" $ example = function () {var_dump ($ message) ;}; echo $ example (); // inherit $ message $ example = function () use ($ message) {var_dump ($ message) ;}; echo $ example ();

HereuseWhat is the mechanism of action? I don't know much about it!

Reply content:

The document provides an example of an anonymous function.

The closure can inherit variables from the parent scope. Any such variables should be passed in the use language structure.

$ Message = 'hello'; // No "use" $ example = function () {var_dump ($ message) ;}; echo $ example (); // inherit $ message $ example = function () use ($ message) {var_dump ($ message) ;}; echo $ example ();

HereuseWhat is the mechanism of action? I don't know much about it!

HereuseThe mechanism is not just an anonymous function, but more clearly definedClosureClosures are generally implemented through anonymous functions.

HereuseIn fact$messageCopy to a local function, so it is similarglobalBut unlike global:

useWhen you are in the function$messageThe change will not affect the global one.$messageIn other words, useuseActually, a copy of the variable is copied to the function.

globalTime: opposite, in the function$messageThe Global One will be affected during the change.$message.

For more details, refer to my blog on PHP anonymous functions and closures.

It's equivalent to global.

function() {    global $message;    var_dump($message);}

If you are unfamiliar with anonymous functions, you may want to look at the code below:

$ Message = 'hello'; // No "use" $ example = function () {var_dump ($ message) ;}; echo $ example (); // inherit $ message $ example = function ($ message) {global $ message; var_dump ($ message) ;}; echo $ example ($ message );

The output result is the same as the anonymous function under the use mechanism."Get the variables in the parent scope to the sub-scope for use ".
The reason why we need to specifically declare use or global is that the variable scope in PHP functions is partial, unlike JavaScript, which allows a layer-by-layer query to the outer scope.

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.