Heim >Backend-Entwicklung >PHP-Tutorial >深入理解PHP内核(五)函数的内部结构,深入理解内部结构_PHP教程
php的函数包括用户定义的函数、内部函数(print_r count...)、匿名函数、变量函数($func = 'print_r'; $func(array('a','b'));)
PHP内核源码中将函数分为以下类型
<span>#define</span> ZEND_INTERNAL_FUNCTION 1 <span>#define</span> ZEND_USER_FUNCTION 2 <span>#define</span> ZEND_OVERLOADED_FUNCTION 3 <span>#define</span> ZEND_EVAL_CODE 4 <span>#define</span> ZEND_OVERLOADED_FUNCTION_TEMPORARY 5
一、用户函数(ZEND_USER_FUNCTION)
函数不一定显式的有返回值,在PHP的实现中即使没有显式的返回,PHP内核也会帮我们返回NULL。
ZEND在执行过程中,会将运行时信息存储于_zend_execute_data中:
<span>struct</span><span> _zend_execute_data { </span><span>//</span><span>...省略部分代码</span> <span> zend_function_state function_state; zend_function </span>*fbc; <span>/*</span><span> Function Being Called </span><span>*/</span> <span>//</span><span>...省略部分代码</span> };
在程序初始化的过程中,function_state也会进行初始化,function_state由两个部分组成:
typedef <span>struct</span><span> _zend_function_state { zend_function </span>*<span>function; </span><span>void</span> **<span>arguments; } zend_function_state;</span>
*arguments是一个指向函数参数的指针,而函数体本事存储于*function中,*function是一个zend_function结构体,它最终存储了用户自定义函数的一切信息,具体结构如下:
<span>typedef union _zend_function { zend_uchar type; </span><span>/*</span><span> MUST be the first element of this struct! </span><span>*/</span> <span>struct</span><span> { zend_uchar type; </span><span>/*</span><span> never used </span><span>*/</span> <span>char</span> *function_name; <span>//</span><span>函数名称</span> zend_class_entry *scope; <span>//</span><span>函数所在的类作用域</span> zend_uint fn_flags; <span>//</span><span>函数类型,如用户自定义则为 #define </span> ZEND_USER_FUNCTION <span>2</span><span> union _zend_function </span>*prototype; <span>//</span><span>函数原型</span> zend_uint num_args; <span>//</span><span>参数数目</span> zend_uint required_num_args; <span>//</span><span>需要的参数数目</span> zend_arg_info *arg_info; <span>//</span><span>参数信息指针</span> <span> zend_bool pass_rest_by_reference; unsigned </span><span>char</span> return_reference; <span>//</span><span>返回值</span> <span> } common; zend_op_array op_array; </span><span>//</span><span>函数中的操作</span> <span> zend_internal_function internal_function; } zend_function;</span>
zend_function的结构体中的op_array存储了该函数中的所有操作,当函数被调用时,ZEND就会将这个op_array中的opline一条条顺序执行,并将最后的结果返回。函数的定义和执行是分开的,一个函数可以作为一个独立的运行单元存在。
二、内部函数(ZEND_INTERNAL_FUNCTION)
ZEND_INTERNAL_FUNCTION函数是由扩展或者Zend/PHP内核提供的,用c/c++编写,可以直接执行的函数,以下为内部函数的结构
typedef <span>struct</span><span> _zend_internal_function { </span><span>/*</span><span> Common elements </span><span>*/</span><span> zend_uchar type; </span><span>char</span> *<span> function_name; zend_class_entry </span>*<span>scope; zend_uint fn_flags; union _zend_function </span>*<span>prototype; zend_uint num_args; zend_uint required_num_args; zend_arg_info </span>*<span>arg_info; zend_bool pass_rest_by_reference; unsigned </span><span>char</span><span> return_reference; </span><span>/*</span><span> END of common elements </span><span>*/</span> <span>void</span> (*<span>handler)(INTERNAL_FUNCTION_PARAMETERS); </span><span>struct</span> _zend_module_entry *<span>module; } zend_internal_function;</span>
在模块初始化的时候,ZE会遍历每个载入的扩展模块,然后将模块中function_entry中指明的每一个函数(module->functions),创建一个zend_internal_function结构,并将其type设置为ZEND_INTERNAL_FUNCTION,将这个结构填入全局的函数表(HashTable结构);函数设置及注册过程见Zend/zene_API.c文件中的zend_register_function函数,这个函数除了处理函数页也处理类的方法,包括那些魔术方法。
内部函数的结构与用户自定义函数结构基本类似,有一些不同:
三、变量函数
如果一个变量名后边有圆括号,php将寻找与变量的值同名的函数,并且尝试执行。
变量函数$func
$func = <span>'</span><span>print_r</span><span>'</span><span>; $func(</span><span>'</span><span>i am print_r function.</span><span>'</span>);
编译后中间代码
function name: (<span>null</span><span>) number of ops: </span><span>9</span><span> compiled vars: </span>!<span>0</span> =<span> $func line # </span>* op fetch ext <span>return</span><span> operands </span>------------------------------------------------------------------------------ - - <span>2</span> <span>0</span> ><span> EXT_STMT </span><span>1</span> ASSIGN !<span>0</span><span>, </span><span>'</span><span>print_r</span><span>'</span> <span>3</span> <span>2</span><span> EXT_STMT </span><span>3</span> INIT_FCALL_BY_NAME !<span>0</span> <span>4</span><span> EXT_FCALL_BEGIN </span><span>5</span><span> SEND_VAL </span><span>'</span><span>i+am+print_r+function.</span><span>'</span> <span>6</span> DO_FCALL_BY_NAME <span>1</span> <span>7</span><span> EXT_FCALL_END </span><span>8</span> > RETURN 1
内部函数
print_r(<span>'</span><span>i am print_r function.</span><span>'</span>);
编译后中间代码
function name: (<span>null</span><span>) number of ops: </span><span>6</span><span> compiled vars: none line # </span>* op fetch ext <span>return</span><span> operands </span>------------------------------------------------------------------------------- - - <span>2</span> <span>0</span> ><span> EXT_STMT </span><span>1</span><span> EXT_FCALL_BEGIN </span><span>2</span><span> SEND_VAL </span><span>'</span><span>i+am+print_r+function.</span><span>'</span> <span>3</span> DO_FCALL <span>1</span> <span>'</span><span>print_r</span><span>'</span> <span>4</span><span> EXT_FCALL_END </span><span>5</span> > RETURN <span>1</span>
对比发现,二者在调用中间代码上存在一些区别,变量函数是DO_FCALL_BY_NAME,而内部函数是DO_FCALL。这在语法解析时就已经决定了,见Zend/zend_complie.c文件的zend_do_end_function_call函数中部分代码:
<span>if</span> (!is_method && !is_dynamic_fcall && function_name->op_type==<span>IS_CONST) { opline</span>->opcode =<span> ZEND_DO_FCALL; opline</span>->op1 = *<span>function_name; ZVAL_LONG(</span>&opline-><span>op2.u.constant, zend_hash_func(Z_STRVAL(function_name</span>->u.constant), Z_STRLEN(function_name- >u.constant) + <span>1</span><span>)); } </span><span>else</span><span> { opline</span>->opcode =<span> ZEND_DO_FCALL_BY_NAME; SET_UNUSED(opline</span>-><span>op1); }</span>
如果不是方法,并且不是动态调用,并且函数名为字符串变量,则其生成的中间代码为ZEND_DO_FCALL。其他情况则为ZEND_DO_FCALL_BY_NAME。另外将变量函数作为回调函数,其处理过程在Zend/zend_complie.c文件的zend_do_pass_param函数中,最终会体现在中间代码执行过程中的ZEND_SEND_VAL_SPEC_CONST_HADNLER等函数中。
四、匿名函数
匿名函数是一类不需要指定表示符,而又可以被调用的函数或子例程,匿名函数可以方便的作为参数传递给其他函数。