An in-depth understanding of the PHP principles Function (Introspecting PHP Function)

Source: Internet
Author: User
Tags types of functions

In PHP, there are two types of functions,

  • One is zend_internal_function, which is provided by the extension or Zend/PHP kernel and can be directly executed using 'C/C ++.
  • The other is zend_user_function, which we often see. The function defined by the user in the PHP script will eventually be translated into opcode array for execution by ZE.

    To view zend_compile.h, we can find the following three structures:

      
      
    1. typedef struct _zend_internal_function {
    2.     /* Common elements */
    3.     zend_uchar type;
    4.     char * function_name;
    5.     zend_class_entry *scope;
    6.     zend_uint fn_flags;
    7.     union _zend_function *prototype;
    8.     zend_uint num_args;
    9.     zend_uint required_num_args;
    10.     zend_arg_info *arg_info;
    11.     zend_bool pass_rest_by_reference;
    12.     unsigned char return_reference;
    13.     /* END of common elements */
    14.     void (*handler)(INTERNAL_FUNCTION_PARAMETERS);
    15.     struct _zend_module_entry *module;
    16. } zend_internal_function;
    17. struct _zend_op_array {
    18.     /* Common elements */
    19.     zend_uchar type;
    20.     char *function_name;
    21.     zend_class_entry *scope;
    22.     zend_uint fn_flags;
    23.     union _zend_function *prototype;
    24.     zend_uint num_args;
    25.     zend_uint required_num_args;
    26.     zend_arg_info *arg_info;
    27.     zend_bool pass_rest_by_reference;
    28.     unsigned char return_reference;
    29.     /* END of common elements */
    30.     zend_uint *refcount;
    31.     zend_op *opcodes;
    32.     zend_uint last, size;
    33.     zend_compiled_variable *vars;
    34.     int last_var, size_var;
    35.     zend_uint T;
    36.     zend_brk_cont_element *brk_cont_array;
    37.     zend_uint last_brk_cont;
    38.     zend_uint current_brk_cont;
    39.     zend_try_catch_element *try_catch_array;
    40.     int last_try_catch;
    41.     /* static variables support */
    42.     HashTable *static_variables;
    43.     zend_op *start_op;
    44.     int backpatch_count;
    45.     zend_bool done_pass_two;
    46.     zend_bool uses_this;
    47.     char *filename;
    48.      zend_uint line_start;
    49.     zend_uint line_end;
    50.     char *doc_comment;
    51.     zend_uint doc_comment_len;
    52.     void *reserved[ZEND_MAX_RESERVED_RESOURCES];
    53. };
    54. typedef union _zend_function {
    55.     zend_uchar type; /* MUST be the first element of this struct! */
    56.     struct {
    57.         zend_uchar type; /* never used */
    58.         char *function_name;
    59.         zend_class_entry *scope;
    60.         zend_uint fn_flags;
    61.         union _zend_function *prototype;
    62.         zend_uint num_args;
    63.         zend_uint required_num_args;
    64.         zend_arg_info *arg_info;
    65.         zend_bool pass_rest_by_reference;
    66.         unsigned char return_reference;
    67.     } common;
    68.     zend_op_array op_array;
    69.     zend_internal_function internal_function;
    70. } zend_function;
    71.    

    The first structure defines zend_internal_function. When PHP is started, it traverses each loaded extension module and creates a zend_internal_function structure for each function specified in function_entry in the module, set type to ZEND_INTERNAL_FUNCTION (see the table below), and enter this structure into the global function table (A HashTable );

      
      
    1. #define ZEND_INTERNAL_FUNCTION 1
    2. #define ZEND_USER_FUNCTION 2
    3. #define ZEND_OVERLOADED_FUNCTION 3
    4. #define ZEND_EVAL_CODE 4
    5. #define ZEND_OVERLOADED_FUNCTION_TEMPORARY 5
    6.      

    The second structure, op_array, is very important because:

      
      
    1.       extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
    2.   

    That is to say, all PHP scripts we write will be translated into op_array by ZE and finally executed by zend_execute.

    In addition, in ZE, user-defined functions (userland functions) are also translated into an op_array and filled in the global function table. At this time, scope and function_name are not empty. For the Code directly in the global scope, the scope of the final op_array is global, and function_name is empty.

     

  • The third structure is interesting. To understand this structure, you must first understand its design objectives:

  • Zend_internal_function, zend_function, and zend_op_array can safely convert each other (the are not identical structs, but all the elements that are in "common" they hold in common, thus the can safely be casted to each other );

  • Specifically, when calling a function through zend_do_fcall in OP code, ze will be in the function table according to the name (actually the name of the lowercase function, this is why the PHP function name is case-insensitive.) If a query function is found, a zend_function structure pointer is returned (read the zend_function structure above carefully), and the type is determined, if zend_internal_function is used, ze calls zend_execute_internal to execute this function through zend_internal_function.handler. If not, zend_execute is called to execute the zend_op_array contained in this function.

  • Address: http://www.laruence.com/2008/08/12/164.html
  • 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.