阅读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_tadd_ref;
zend_object_del_ref_tdel_ref;
zend_object_clone_obj_tclone_obj;
/* individual object functions */
zend_object_read_property_tread_property;
zend_object_write_property_twrite_property;
zend_object_read_dimension_tread_dimension;
zend_object_write_dimension_twrite_dimension;
zend_object_get_property_ptr_ptr_tget_property_ptr_ptr;
zend_object_get_tget;
zend_object_set_tset;
zend_object_has_property_thas_property;
zend_object_unset_property_tunset_property;
zend_object_has_dimension_thas_dimension;
zend_object_unset_dimension_tunset_dimension;
zend_object_get_properties_tget_properties;
zend_object_get_method_tget_method;
zend_object_call_method_tcall_method;
zend_object_get_constructor_tget_constructor;
zend_object_get_class_entry_tget_class_entry;
zend_object_get_class_name_tget_class_name;
zend_object_compare_tcompare_objects;
zend_object_cast_tcast_object;
zend_object_count_elements_tcount_elements;
zend_object_get_debug_info_tget_debug_info;
zend_object_get_closure_tget_closure;
};
定义了PHP中对象的结构。
哈哈,是不是收获很多啊,欢迎大家提意见哈。。
|