PHP代码实现3 [函数角度]

内部函数

标准函数的实现存放在ext/standard扩展目录中。

php函数种类

Zend engine中的function的存在形式

1
2
3
4
5
6
struct _zend_execute_data {
//...省略部分代码
zend_function_state function_state;
zend_function *fbc; /* Function Being Called */
//...省略部分代码
};

zend_function 的存在形式 Union!

联合体的所有成员变量共享内存中的一块内存,在某个时刻只能有一个成员使用这块内存, 并且当使用某一个成员时,其仅能按照它的类型和内存大小修改对应的内存空间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
typedef union _zend_function {
zend_uchar type; /* 如用户自定义则为 #define ZEND_USER_FUNCTION 2
MUST be the first element of this struct! */

struct {
zend_uchar type; /* never used */
char *function_name; //函数名称
zend_class_entry *scope; //函数所在的类作用域
zend_uint fn_flags; // 作为方法时的访问类型等,如ZEND_ACC_STATIC等
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;
  • 在PHP的实现中,即使没有显式的返回, Zend引擎也会“帮你“返回NULL。
1
2
3
4
$func = 'print_r';
$func('i am print_r function.');

print_r('i am print_r function.');

变量函数是DO_FCALL_BY_NAME,而内部函数是DO_FCALL。这在语法解析时就已经决定了

如果不是方法,并且不是动态调用,并且函数名为字符串常量,则其生成的中间代码为ZEND_DO_FCALL。其它情况则为ZEND_DO_FCALL_BY_NAME。

posted @ 2018-09-10 23:29  guoguoqingzhe  阅读(269)  评论(0编辑  收藏  举报