In PHP, the Callback method has always been ugly. before PHP5.3, we had two options: 1. string function name 2. the returned value of create_function is later than PHP5.3. we have another option, namely Closure, $ funcfunction (){...}; arr
In PHP, 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 ~
Note: this article