In-depth understanding of the internal structure of the PHP kernel (v) functions, in-depth understanding of internal structures
PHP functions include user-defined functions, intrinsic functions (print_r count ...), anonymous functions, variable functions ($func = ' Print_r '; $func (Array (' A ', ' B ');)
PHP kernel source will be divided into the following types of functions
#define Zend_internal_function 1#define zend_user_function 2 #define zend_overloaded_ FUNCTION 3#define zend_eval_code 4#define zend_overloaded_function_temporary 5
First, user function (zend_user_function)
The function does not necessarily have a return value explicitly, and the PHP kernel will return NULL for the PHP implementation even if it is not explicitly returned.
Zend The run-time information is stored in _zend_execute_data during execution:
struct _zend_execute_data { //... Omit some code zend_function_state function_state; /* */ //... Omit part of the Code};
During program initialization, Function_state is also initialized, and the function_state consists of two parts:
struct _zend_function_state { *function; void * *arguments;} zend_function_state;
*arguments is a pointer to a function parameter, and the function body ability is stored in *function, *function is a zend_function structure, it finally stores the user-defined function of all information, the structure is as follows:
typedef Union _zend_function {Zend_uchar type; /*must is the first element of this struct!*/ struct{Zend_uchar type; /*never used*/ Char*function_name;//Function NameZend_class_entry *scope;//the scope of the class where the function residesZend_uint Fn_flags;//function type, such as user-defined #defineZend_user_function2Union _zend_function*prototype;//function PrototypesZend_uint Num_args;//Number of parametersZend_uint Required_num_args;//number of parameters requiredZend_arg_info *arg_info;//parameter information pointersZend_bool pass_rest_by_reference; unsignedCharReturn_reference;//return value} Common; Zend_op_array Op_array; //actions in the functionzend_internal_function internal_function; } zend_function;
The Op_array in the zend_function structure stores all the operations in the function, and when the function is called, Zend executes the opline in this Op_array and returns the final result. The definition and execution of a function is separate, and a function can exist as a separate running unit.
Second, intrinsic function (zend_internal_function)
The Zend_internal_function function is a function that is provided by the extension or zend/php kernel, which can be executed directly by C + +, and the following is the structure of the inner function
typedef struct _zend_internal_function { /* Common Elements */ Zend_uchar type; Char * function_name; Zend_class_entry * scope; Zend_uint Fn_flags; Union _zend_function * prototype; Zend_uint Num_args; Zend_uint Required_num_args; Zend_arg_info * arg_info; Zend_bool pass_rest_by_reference; unsigned char return_reference; /* END of common Elements */ void (* handler) (INTERNA L_function_parameters); struct _zend_module_entry * module;} zend_internal_function;
When the module is initialized, ze iterates through each loaded extension module and then creates a zend_internal_function structure for each function (module->functions) specified in Function_entry in the module. And the type is set to Zend_internal_function, the structure is filled into the global function table (hashtable structure); Function setup and registration process see Zend_register_ in ZEND/ZENE_API.C file The function function, which handles the methods of the class in addition to the function page, including those magic methods.
The structure of internal functions is basically similar to the structure of a user-defined function, with some differences:
- Call method, Handler field, if Zend_internal_function, then Zend will call Zend_execute_internal, through Zend_internal_ Function.handler to execute this function. The user-defined function needs to generate intermediate code, which is then mapped to a relative by the intermediate code to invoke the method.
- Built-in functions have a module field in the structure that represents which modules belong. Different expansion modules are different
- Type field, in a user-defined function, the Type field is almost useless, and the Type field in the built-in function is distinguished as several intrinsic functions.
Third, variable function
If a variable name has parentheses behind it, PHP will look for a function with the same name as the value of the variable and try to execute it.
Variable function $func
' Print_r ' ; $func ('I am print_r function. ');
Post-compilation intermediate code
Function Name: (NULL) Number of OPS:9compiled VARs:!0=$funcline #* OP FETCH extreturnoperands--------------------------------------------------------------------------------2 0>ext_stmt1ASSIGN!0, 'Print_r' 3 2ext_stmt3Init_fcall_by_name!0 4Ext_fcall_begin5Send_val'i+am+print_r+function.' 6Do_fcall_by_name1 7Ext_fcall_end8> RETURN 1
Intrinsic functions
Print_r ('I am print_r function. ');
Post-compilation intermediate code
function name: ( null ) Number of OPS: 6 compiled VARs: Noneline # * OP fetch ext return operands ------ --------------------------------------------------------------------------- 2 0 >< span> ext_stmt 1 ext_fcall_begin 2 Send_va L ' i+am+print_r+function. ' 3 do_fcall 1 ' Print_r ' 4 ext_fcall_end 5 > RETURN 1
The comparison shows that there are some differences between the two in calling the intermediate code, the variable function is do_fcall_by_name, and the intrinsic function is do_fcall. This is determined at the time of parsing, see part of the code in the Zend_do_end_function_call function of the zend/zend_complie.c file:
if (!is_method &&!is_dynamic_fcall && function_name->op_type==is_const) { Opline- opcode = Zend_do_fcall; Opline->op1 = *function_name; Zval_long (&opline->op2.u.constant, Zend_hash_func (Z_strval (function_name1)); Else { opline->opcode = zend_do_fcall_by_name; Set_unused (Opline-OP1); }
If it is not a method and is not a dynamic call, and the function name is a string variable, then its resulting intermediate code is zend_do_fcall. Other cases are zend_do_fcall_by_name. In addition, the variable function as a callback function, its processing in the Zend_do_pass_param function of the zend/zend_complie.c file, will eventually be reflected in the middle code execution process Zend_send_val_spec_const_ Hadnler and other functions.
Iv. Anonymous functions
An anonymous function is a class of functions or subroutines that do not require a specified representation and can be called, and anonymous functions can be easily passed as arguments to other functions.
http://www.bkjia.com/PHPjc/1099824.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099824.html techarticle In- depth understanding of the internal structure of the PHP kernel (v) function, in-depth understanding of the internal structure PHP functions include user-defined functions, intrinsic functions (print_r count ...), anonymous functions, variable functions ($f ...