In-depth understanding of the internal structure of the PHP kernel (v) function

Source: Internet
Author: User
Tags types of functions vars
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;    Zend_function *FBC; /* Function Being called *    //... Omit part of the code};

During program initialization, Function_state is also initialized, and the function_state consists of two parts:

typedef struct _ZEND_FUNCTION_STATE {    zend_function *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 name        zend_class_entry *scope;//The class scope where the function is        zend_uint fn_flags;     The function type, such as user-defined, is #define ZEND_USER_FUNCTION 2          Union _zend_function *prototype;//function prototype        zend_uint Num_args;     Number of parameters        Zend_uint Required_num_args;//number of parameters required        Zend_arg_info *arg_info;  Parameter information pointer        zend_bool pass_rest_by_reference;        unsigned char return_reference;  return value    } common;     Zend_op_array Op_array;   The operation in the function    zend_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) (internal_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

    $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          ext  return operands--------------------------------------------------------------------------------   2     0  >   ext_stmt         1      ASSIGN                                                   ! 0, ' Print_r '   3     2      ext_stmt         3      init_fcall_by_name                                       !0         4      ext_fcall_begin         5      send_val                                                 ' i+am+print_r+function. '         6      do_fcall_by_name                              1         7      ext_fcall_end         8    > RETURN        1

    Intrinsic functions

    Print_r (' I am print_r function. ');

    Post-compilation intermediate code

    Function name:  (NULL) Number of OPS:  6compiled vars:  noneline     # *  op                           fetch          ext  return  operands---------------------------------------------------------------------------------   2     0  >   ext_stmt         1      ext_fcall_begin         2      send_val                                                 ' 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_name->u.constant), Z_STRLEN ( Function_name->u.constant) + 1));    } 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.

      

  • Related Article

    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.