PHP Source function

Source: Internet
Author: User

In PHP, functions are divided into two types,

    • One is zend_internal_function, a function that is provided by the extension or zend/php kernel, written in ' C/s + + ', and can be executed directly.
    • The other is zend_user_function, which is what we often see, the function that the user defines in the PHP script, which will eventually be translated into the opcode array by ze to execute

Looking at zend_compile.h, we can find the following 3 structures:

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;

struct _zend_op_array {

/* 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 */

Zend_uint *refcount;

Zend_op *opcodes;

Zend_uint last, size;

Zend_compiled_variable *vars;

int Last_var, Size_var;

Zend_uint T;

Zend_brk_cont_element *brk_cont_array;

Zend_uint last_brk_cont;

Zend_uint current_brk_cont;

Zend_try_catch_element *try_catch_array;

int Last_try_catch;

/* Static variables support */

HashTable *static_variables;

Zend_op *start_op;

int Backpatch_count;

Zend_bool done_pass_two;

Zend_bool uses_this;

Char *filename;

Zend_uint line_start;

Zend_uint line_end;

Char *doc_comment;

Zend_uint Doc_comment_len;

void *reserved[zend_max_reserved_resources];

};

typedef Union _zend_function {

Zend_uchar type; / * must is the first element of this struct! */

struct {

Zend_uchar type; / * never used * /

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;

} common;

Zend_op_array op_array;

Zend_internal_function internal_function;

} zend_function;

The first structure defines the zend_internal_function, which, when PHP is started, iterates through each loaded extension and then creates a Zend_internal_ for each function indicated in Function_entry in the module. function structure, and set the type to zend_internal_function (see table below), fill the structure into the Global functions table (a Hashtable);

#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

The second structure, Op_array, is important because:

extern Zend_api zend_op_array * (*zend_compile_file) (zend_file_handle *file_handle , int type tsrmls_dc);

In other words, the PHP script we write will be translated into Op_array by Ze and finally executed by Zend_execute.

In addition, in ze, user-defined functions (Userland function) are also translated into a op_array and filled into the global function table. Scope,function_name is not empty at this time. For direct code in the global scope, the last Op_array scope is global and function_name is empty.

The third structure, very interesting, to understand this structure, first of all you have to understand his design goals: Zend_internal_function, Zend_function,zend_op_array can be safely converted to each other (the is not identical structs, but all the elements that is in "common" they hold in common, thus the can safely is casted to each other);

Specifically, when calling a function through Zend_do_fcall in op code, ze in the function table, according to the name (actually lowercase function name, which is why the function name of PHP is case insensitive) lookup function, if found, Return a pointer to a zend_function structure (look closely at the zend_function structure above), then determine the type, and if it is zend_internal_function, then ze calls Zend_execute_ Internal, execute this function through Zend_internal_function.handler, and if not, call Zend_execute to execute the Zend_op_array that the function contains.

PHP Source function

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.