Deep understanding of anonymous functions in PHP

Source: Internet
Author: User
Tags anonymous array bool closure count eval functions return

The function of the anonymous function is to enlarge the function, and before PHP 5.3, we have only two choices to pass the callback way:

The function name of the string

Using Create_function's return

After PHP5.3, we have one more choice, namely closure.

    1. 		$func = function () {...};
    2. Array_walk ($arr, $func);

The first way to implement this is to pass the function name string as the simplest. And the second way create_function, in fact, and the first way essentially the same, Create_function returns the function name of a string, which is the format of the function name:

    1. 		"\000_lambda_". Count (anonymous_functions) + +

Let's look at the create_function implementation steps:

1. Get the parameter, function body;

2. Piecing together a "function __lambda_func (parameter) {functional body;} "The string;

3. Eval;

4. Through __lambda_func in the function table to find the function of the eval, can not find the error;

5. Define a function name: "\000_lambda_". Count (anonymous_functions) + +;

6. Replace the __lambda_func with the new function name;

7. Returns the new function.

Let's verify the following:

    1. 		<?php
    2. Create_function ("", ' echo __function__; ');
    3. Call_user_func ("\000lambda_1", 1);
    4. ?>
    5. Output
    6. __lambda_fun

Because the function name is "__lambda_func" at Eval, the __lambda_func is output within the anonymous function, and because the last "\000_lambda_". Count (anonymous_functions) + + Renamed the "__lambda_func" function in the function table, so you can pass "\000_lambda_". Count (anonymous_functions) + + calls this anonymous function. To confirm this, you can dump the return value of create_function to view it.

When PHP 5.3 was released, one of the new feature was to support the closure/lambda Function, and my first reaction was to assume that Zval added a is_function, but it actually constructed a closure "class" introduced by PHP 5.3. instance, the constructor for the closure class is private, so it cannot be instantiated directly, and the closure class is the final class, so it cannot be derived as a base class.

    1. 		php-5.3.0
    2. $class = new Reflectionclass ("Closure");
    3. Var_dump ($class->isinternal ());
    4. Var_dump ($class->isabstract ());
    5. Var_dump ($class->isfinal ());
    6. Var_dump ($class->isinterface ());
    7. Output:
    8. BOOL (TRUE)
    9. BOOL (FALSE)
    10. BOOL (TRUE)
    11. BOOL (FALSE)
    12. ?>

In PHP 5.3, the support for closures is simply to keep the external variables as "static attributes" of the closure object (not a normal traversal/access attribute).

    1. 		php-5.3.0
    2. $b = "Laruence";
    3. $func = function ($a) use ($b) {};
    4. Var_dump ($func);
    5. /* Output:
    6. Object (Closure) #1 (2) {
    7. ["Static"]=>
    8. Array (1) {
    9. ["B"]=>
    10. String (8) "Laruence"
    11. }
    12. ["Parameter"]=>
    13. Array (1) {
    14. ["$a"]=>
    15. String (a) "<required>"
    16. }
    17. }
    18. */

This implementation, personally think and JS to the closure of the package, or a bit too shabby.



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.