阅读PHP内核系统Zend引擎源码手记(原创)

阅读PHP内核系统Zend引擎源码手记(原创)
作者:余超 EMAIL:yuchao86@gmail.com

周末在家没事,一个人读内核,
在文件Zend/zend_object_handlers.h文件的94-100行出现如下代码,很多人不明其义,
一开始我也一头迷茫,后来仔细分析了一下,写下心得。
/* Object maintenance/destruction */
typedef void (*zend_object_add_ref_t)(zval *object TSRMLS_DC);
typedef void (*zend_object_del_ref_t)(zval *object TSRMLS_DC);
typedef void (*zend_object_delete_obj_t)(zval *object TSRMLS_DC);
typedef zend_object_value (*zend_object_clone_obj_t)(zval *object TSRMLS_DC);

首先,简单来讲,typedef是定义一个别名,typedef int Intger; 那么Intger就可以定义一个整型变量。
同样,void (*terminate_handler) ();是一个函数指针的声明.就是声明一个指向函数的指针,指向没有参数,返回值为void的函数.如void fun(),可以用这个指针来指向fun函数.即terminate_handler=fun;//这就表示指针terminate_handler指向了fun()函数.但是如果有函数void fun1(int a)或int fun2(),如果试图用terminate_handler指针来指向这两个函数,即terminate_handler=fun1或terminate_handler=fun2就会出错.
而这样:
typedef void (*terminate_handler) ();
typedef是把这种类型的指针类型命名为terminate_handler
即可通过terminate_handler来生成一个函数指针.如terminate_handler p;p=fun;
terminate_handler 就成了一种类型.

如下是英文版的解说:
typedef   type-declaration   synonym; 

The   typedef   keyword   defines   a   synonym   for   the   specified   type-declaration.   The   identifier   in   the   type-declaration   becomes   another   name   for   the   type,   instead   of   naming   an   instance   of   the   type.   You   cannot   use   the   typedef   specifier   inside   a   function   definition. 


A   typedef   declaration   introduces   a   name   that,   within   its   scope,   becomes   a   synonym   for   the   type   given   by   the   decl-specifiers   portion   of   the   declaration.   In   contrast   to   the   class,   struct,   union,   and   enum   declarations,   typedef   declarations   do   not   introduce   new   types   —   they   introduce   new   names   for   existing   types. 

Example 

//   Example   of   the   typedef   keyword 
typedef   unsigned   long   ulong; 


ulong   ul;           //   Equivalent   to   "unsigned   long   ul; " 

typedef   struct   mystructtag 

      int       i; 
      float   f; 
      char     c; 
}   mystruct; 

mystruct   ms;       //   Equivalent   to   "struct   mystructtag   ms; " 

typedef   int   (*funcptr)();     //   funcptr   is   synonym   for   "pointer 
                                                      //         to   function   returning   int " 

funcptr   table[10];       //   Equivalent   to   "int   (*table[10])(); " 

所以你就可以在zend引擎文件zend/zend_API.h中看到很多类似的定义了
struct _zend_object_handlers {
/* general object functions */
zend_object_add_ref_t add_ref;
zend_object_del_ref_t del_ref;
zend_object_clone_obj_t clone_obj;
/* individual object functions */
zend_object_read_property_t read_property;
zend_object_write_property_t write_property;
zend_object_read_dimension_t read_dimension;
zend_object_write_dimension_t write_dimension;
zend_object_get_property_ptr_ptr_t get_property_ptr_ptr;
zend_object_get_t get;
zend_object_set_t set;
zend_object_has_property_t has_property;
zend_object_unset_property_t unset_property;
zend_object_has_dimension_t has_dimension;
zend_object_unset_dimension_t unset_dimension;
zend_object_get_properties_t get_properties;
zend_object_get_method_t get_method;
zend_object_call_method_t call_method;
zend_object_get_constructor_t get_constructor;
zend_object_get_class_entry_t get_class_entry;
zend_object_get_class_name_t get_class_name;
zend_object_compare_t compare_objects;
zend_object_cast_t cast_object;
zend_object_count_elements_t count_elements;
zend_object_get_debug_info_t get_debug_info;
zend_object_get_closure_t get_closure;
};
定义了PHP中对象的结构。
哈哈,是不是收获很多啊,欢迎大家提意见哈。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值