The content of this article is about PHP7 kernel anatomy 6 function, now share to everyone, the need for friends can refer to
1. Storage structure of functions
typedef Union _zend_function zend_function;union _zend_function { Zend_uchar type; struct { Zend_uchar type; Zend_uchar arg_flags[3]; uint32_t fn_flags; Zend_string *function_name; Zend_class_entry *scope; The Member method belongs to the class, the object-oriented implementation of the union _zend_function *prototype; uint32_t Num_args; Number of parameters uint32_t Required_num_args;//required number of parameters Zend_arg_info *arg_info;//parameter information } common; Zend_op_array Op_array; A custom function (the function is actually compiled into a normal zend_op_array) zend_internal_function internal_function;//intrinsic function (via extension or C function provided by the kernel)};
ZEND_FUNCTION.COMMON.XX quick access to zend_function.zend_op_array.xx and Zend_function.zend_internal_function.xxzend_ Function.type take to Zend_function.op_array.type and Zend_function.internal_function.type
the Function_table property of eg is a hash table that records all the functions in PHP
2. Function parameters
The
function parameter is actually the same as the local variable inside the function in the kernel implementation, when the function call first copies the value of the parameter to the respective position of the parameter at the call location
Additional information for parameters typedef struct _ZEND_ARG_INFO { zend_string *name;//Parameter name zend_string *class_name; Zend_uchar Type_hint; Explicitly declared parameter types, such as (array $param _1) Zend_uchar pass_by_reference;//whether to reference the parameter, the value of the pre-& is 1 zend_bool Allow_ Null Whether to allow null zend_bool is_variadic;//is a variable parameter, i.e. ... Usage, function my_func ($a, ... $b) {...}} Zend_arg_info;
all of the above parameter results are saved in the Zend_op_array.arg_info array, and if the function declares a return value type it will also create a zend_arg_info for it, which is in the first position of the Arg_info array, in which case zend_op The _array->arg_info point is actually the second position of the array, and the structure of the return value is read by zend_op_array->arg_info[-1]
3. Intrinsic functions
The
intrinsic function refers to the function written by the kernel, the Extended C language, which does not have to go through the opcode compilation process, so it is more efficient than the PHP user-defined functions, and is not different from normal C programs when called. Many internal functions are defined in the Zend engine for use by users in PHP, such as define, defined, strlen, method_exists, class_exists, function_exists ... And so on, in addition to the intrinsic functions defined in the Zend engine, there are a number of intrinsic functions available in the PHP extension, and we can flexibly customize them by extension.
//zend_internal_function Head is a zend_op_array structure typedef struct with exactly the same common zend_internal_function {/* Common elements */Zend_uchar type; Zend_uchar Arg_flags[3]; /* Bitset of Arg_info.pass_by_reference */uint32_t fn_flags; zend_string* function_name; Zend_class_entry *scope; Zend_function *prototype; uint32_t Num_args; uint32_t Required_num_args; Zend_internal_arg_info *arg_info; /* END of common Elements */void (*handler) (internal_function_parameters); function pointers, expanded: void (*handler) (Zend_execute_data *execute_data, zval *return_value) struct _zend_module_entry *module; void *reserved[zend_max_reserved_resources];} zend_internal_function;