What is the use of PHP anonymous functions?

Source: Internet
Author: User

Use in anonymous functions , which is the function of inheriting variables from the parent scope.

The following example is the most common usage, and if use is not used, the variable $msg will not be found in the function.

<?php$msg = [n/a]; $func = function () use ($msg) {    print_r ($msg);};  $func ();? >

Run output

Array (    [0] = 1    [1] = 2    [2] = 3)

About the time to inherit variables

Does the behavior of an inherited variable occur when a function is defined or when a function is called? We adjust the order of the code in the example above, placing the $msg after the function definition.

<?php$func = function () use ($msg) {    print_r ($msg);};  $msg = [n/a]; $func ();? >

Run output

PHP Notice:  Undefined variable:msg in/search/ballqiu/c.php on line 4

As can be seen, the behavior of an inherited variable is generated when the function is defined. Defined in the example above

MSG, so the function runtime $msg is undefined variable.

about using reference passing values in use

We know that in the use of anonymous functions if the reference value is used, the change in the value of the parameter in the anonymous function will also affect the external corresponding variable. For example, the following:

<?php$msg = [n/a]; $func = function () use (& $msg) {    $msg [0]++;    Print_r ($msg);}; $func ();p rint_r ($msg);? >

Run output

Array (    [0] = 2    [1] = 2    [2] + 3) array (    [0] = 2    [1] = 2    [2] = 3)

So is it true that in any case, to change the value of an external variable through an anonymous function, you must pass the value to use by reference? Look at the following example:

<?php$msg = new Arrayobject ([], arrayobject::array_as_props), $func = function () use ($msg) {    $msg [0]++;    Print_r ($msg);}; $func ();p rint_r ($msg);? >

Run output

Arrayobject Object (    [storage:ArrayObject:private] = = Array        (            [0] = 2            [1] = 2            [2] = 3        )) Arrayobject Object (    [storage:ArrayObject:private] = = Array        (            [0] = 2            [1] = 2            [2] = = 3        ))

It can be seen that if you pass a variable of type object, even if you do not display the use of reference passing, the change in the value of the variable in the anonymous function also affects the external correlation variable.

But the problem comes again. When passing an object variable to use, is there a difference between using a reference and not using a reference? Or look at an example

<?php$func = function () use ($msg) {    echo $msg [0], "\ n";}; $msg = new Arrayobject ([], arrayobject::array_as_props); $func ();? >

Instead, we use reference delivery

$func = function () use (& $msg) {    echo $msg [0], "\ n";}; Run Output 1

Visible when passing by reference, even if the variable lags behind the function definition, the external corresponding variable can be found inside the function, and there is no case where the variable is undefined. There is a difference between the two.

About this and use in anonymous functions in class

<?phpclass c{    protected $_num = 0;    Public Function Mkfunc () {        $func = function () {            echo $this->_num++, "\ n";        };        return $func;    }    Public function Get () {        echo $this->_num, "\ n";    }} $obj = new C (); $func = $obj->mkfunc (); $func (); $obj->get (); > Running Results 01

Visible in the anonymous function refers to the current object, do not need to use the application can be directly found.

Or the above example, what would be the effect of using use?
Change Mkfunc to

Public Function Mkfunc () {    //The only change is where use is added    $func = function () use ($this) {        echo $this->_num++, "\ n";    } ;    return $func;} Run output PHP Fatal error:  cannot use $this as lexical variable

Revision changed to

Public Function Mkfunc () {    $self = $this;    $func = function () use ($self) {        echo $this->_num++, "\ n";    };    return $func;} Running Results 01

Visible whether use is used, the effect is the same.

Related Article

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.