Example of PHP5 anonymous function-PHP source code

Source: Internet
Author: User
The anonymous function editor rarely uses php anonymous functions during development. However, anonymous functions are used more often in js, next I will introduce the usage of anonymous functions in php. The anonymous function editor rarely uses php anonymous functions during development. However, anonymous functions are used more often in js, next I will introduce the usage of anonymous functions in php.

Script ec (2); script


PHP5.3 supports anonymous functions and can be used in the production environment with peace of mind. Many new PHP features should be used in bold, which makes the code more concise and easier to implement functions.

The following is a simple example of a PHP anonymous function:

Function func ($ list, $ u_wangwang)
{
// Anonymous function to obtain other contact numbers
$ Other_num = function ($ num ){
$ Num = explode ('.', $ num );
Return $ num [0];
};
$ Res = $ other_num ($ v ['SB _ other_num ']);
Return $ res;
}

First, an anonymous function is defined in the method, with a parameter, and then the parameter is passed during the call.


In HP, the Callback method has always been ugly. Before PHP5.3, we had two options:

1. Function Name of the string
2. return values using create_function
After PHP5.3, we have another option, namely Closure,

$ Func = function (){...};
Array_walk ($ arr, $ func );
In terms of implementation, the first method: passing the function name string is the simplest.

The second method, create_function, is essentially the same as the first method. create_function returns the function name of a string. The format of this function name is:

"\ 000_lambda _". count (anonymous_functions) ++;
Let's take a look at the implementation steps of create_function:

1. Get parameters and function bodies
2. piece together a string of "function _ lambda_func (parameter) {function body ;}"
3. eval
4. Find the function body after eval in the function table through _ lambda_func, and an error will occur if eval is not found.
5. Define a function name: "\ 000_lambda _". count (anonymous_functions) ++
6. Replace _ lambda_func with the new function name
7. Return the new function name.
Let's verify:

Create_function ("", 'echo _ FUNCTION __;');
Call_user_func ("\ 000lambda_1", 1 );
?>
// Output
_ Lambda_func
Because the function name is "_ lambda_func" during eval, the _ lambda_func will be output in the anonymous function, and "\ 000_lambda _" will be used at the end _". count (anonymous_functions) ++ renames the "_ lambda_func" function in the function table, so you can use "\ 000_lambda _". count (anonymous_functions) ++ calls this anonymous function.

To verify this, you can dump the returned value of create_function to view it.

When PHP5.3 was released, one of the new feature is to support closures/Lambda functions. My first reaction was that zval added an IS_FUNCTION, however, it actually constructs a Closure "class" instance introduced by PHP5.3. The Closure class constructor is private, so it cannot be directly instantiated. In addition, the Closure class is a Final class, therefore, it cannot be a subclass derived from the base class.

// Php-5.3.0
$ Class = new ReflectionClass ("Closure ");
Var_dump ($ class-> isInternal ());
Var_dump ($ class-> isAbstract ());
Var_dump ($ class-> isFinal ());
Var_dump ($ class-> isInterface ());
// Output:
Bool (true)
Bool (false)
Bool (true)
Bool (false)
?>

PHP5.3 only supports the Closure as an external variable to be maintained as the "Static property" of the Closure object (not a property that can be traversed/accessed in the general sense ).

// Php-5.3.0
$ B = "laruence ";
$ Func = function ($ a) use ($ B ){};
Var_dump ($ func );
/* Output:
Object (Closure) #1 (2 ){
["Static"] =>
Array (1 ){
["B"] =>
String (8) "laruence"
}
["Parameter"] =>
Array (1 ){
["$ A"] =>
String (10 )" "
}
}
*/
This implementation, I personally think, is a little too simple compared with JS's support for closures ~

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.