The implementation of PHP function anonymous function and closure

Source: Internet
Author: User
PHP supports callback functions (callback), which is a much more powerful feature than other high-level languages. However, compared with JavaScript, php5.3 's previous callback function is not very flexible, only function Name of the stringand use create_functionReturn value of two choices. And after php5.3, we have one more choice-- anonymous functions (Anonymous function), also called closure function (closures), which allows temporary creation of a function that does not have a specified name, and is commonly used as the value of a callback function parameter. Of course, there are other applications as well. In the previous chapters we described in detail the detailed use of the PHP callback function, and this section is about the anonymous or closed-custom function of PHP.

Here, let's take an example of an anonymous or closed-custom function, declare a custom function, assign a function without a name to the variable. Immediately after the variable, enclose the parentheses and pass the parameters, calling out the anonymous function and outputting it.

The sample code for the anonymous function is as follows:

<?php/** Anonymous or Closure function instance */$fun = function ($a) {   echo $a;             Assign a function without a name to a variable $fun}; $fun ("php.cn");        Add parentheses and pass parameters, call up anonymous functions and output?>

Anonymous functions can also be used as values of variables. It is the most common use of anonymous functions to pass anonymous functions directly as parameters to callback functions, and the final table forgets to add semicolons.

An example of the code that calls the callback function with an anonymous function as a parameter is as follows:

<?php/** declaration function callback, need to pass an anonymous function as a parameter */function callback ($back) {echo $back ();             parameter is only a function to call}callback here (function () {        ///Call function and pass directly to an anonymous function echo "Closure data";});? >

An important concept of closures is that external variables can be used in intrinsic functions, and the closure functions and external variables need to be connected using the keyword use, which must be declared on the head of the function or class. The closure function inherits variables from the parent scope and is different from using global variables. Global variables exist in a full-poly range, regardless of which function is currently executing. The parent scope of the closure defines the closure function, not necessarily the function that calls it.

Below we also illustrate with a closure instance that declares the function and passes an anonymous function as a parameter, where the argument is only called when the function is used, and the closure uses the external variable in the inner function with the USE keyword. Call the function and pass in the anonymous function directly.

The usage code for the keyword use is as follows:

<?php/** declaration function callback, need to pass an anonymous function as a parameter */function callback ($back) {echo $back ();             parameter is only a function to be called here} $var = "test data";    An important concept of closures is that an intrinsic function uses an external variable to implement the callback (function () use keyword (& $var) {        ///Call the function and directly pass in an anonymous function echo "closure data: {$var} ";});? >

Note: In the above example, the variable referenced by the use is a copy of the $var, and if you want to fully reference it, add the & symbol as in the example above.

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. Video Tutorial: Anonymous function and Closure implementation: function expression and closure principle

3. PHP real-Combat video tutorial

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.