深入PHP内核之面向对象总结

很久以前看过的,今天总结一下

一、PHP中创建一个类

在PHP中创建一个简单的类是这样的:

<?php

$obj = new test($url)

?>

二、zend_class_entry结构

zend_class_entry是内核中定义的一个结构体,是PHP中类与对象的基础结构类型。

struct _zend_class_entry {

char type;    // 类型:ZEND_INTERNAL_CLASS / ZEND_USER_CLASS

char *name;// 类名称

zend_uint name_length;                  // 即sizeof(name) - 1

struct _zend_class_entry *parent; // 继承的父类

int refcount;  // 引用数

zend_bool constants_updated;

zend_uint ce_flags; // ZEND_ACC_IMPLICIT_ABSTRACT_CLASS: 类存在abstract方法

// ZEND_ACC_EXPLICIT_ABSTRACT_CLASS: 在类名称前加了abstract关键字

// ZEND_ACC_FINAL_CLASS

// ZEND_ACC_INTERFACE

HashTable function_table;      // 方法

HashTable default_properties;          // 默认属性

HashTable properties_info;    // 属性信息

HashTable default_static_members;// 类本身所具有的静态变量

HashTable *static_members; // type == ZEND_USER_CLASS时,取&default_static_members;

// type == ZEND_INTERAL_CLASS时,设为NULL

HashTable constants_table;    // 常量

struct _zend_function_entry *builtin_functions;// 方法定义入口

union _zend_function *constructor;

union _zend_function *destructor;

union _zend_function *clone;

/* 魔术方法 */

union _zend_function *__get;

union _zend_function *__set;

union _zend_function *__unset;

union _zend_function *__isset;

union _zend_function *__call;

union _zend_function *__tostring;

union _zend_function *serialize_func;

union _zend_function *unserialize_func;

zend_class_iterator_funcs iterator_funcs;// 迭代

/* 类句柄 */

zend_object_value (*create_object)(zend_class_entry *class_type TSRMLS_DC);

zend_object_iterator *(*get_iterator)(zend_class_entry *ce, zval *object,

intby_ref TSRMLS_DC);

/* 类声明的接口 */

int(*interface_gets_implemented)(zend_class_entry *iface,

zend_class_entry *class_type TSRMLS_DC);

/* 序列化回调函数指针 */

int(*serialize)(zval *object, unsignedchar**buffer, zend_uint *buf_len,

zend_serialize_data *data TSRMLS_DC);

int(*unserialize)(zval **object, zend_class_entry *ce, constunsignedchar*buf,

zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC);

zend_class_entry **interfaces;  //  类实现的接口

zend_uint num_interfaces;  //  类实现的接口数

char *filename; //  类的存放文件地址 绝对地址

zend_uint line_start;  //  类定义的开始行

zend_uint line_end; //  类定义的结束行

char *doc_comment;

zend_uint doc_comment_len;

struct _zend_module_entry *module; // 类所在的模块入口:EG(current_module)

};

二、访问控制

//fn_flags代表可以在定义方法时使用,zend_property_info.flags代表可以在定义属性时使用,ce_flags代表在定义zend_class_entry时候可用

//ZEND_ACC_CTOR 构造函数掩码 , ZEND_ACC_DTOR  析构函数掩码

//ZEND_ACC_STATIC static函数掩码

//ZEND_ACC_ABSTRACT abstract函数掩码

#define ZEND_ACC_STATIC                    0x01    /* fn_flags, zend_property_info.flags */

#define ZEND_ACC_ABSTRACT                  0x02    /* fn_flags */

#define ZEND_ACC_FINAL                      0x04    /* fn_flags */

#define ZEND_ACC_IMPLEMENTED_ABSTRACT      0x08    /* fn_flags */

#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS    0x10    /* ce_flags */

#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS    0x20    /* ce_flags */

#define ZEND_ACC_FINAL_CLASS                0x40    /* ce_flags */

#define ZEND_ACC_INTERFACE                  0x80    /* ce_flags */

#define ZEND_ACC_INTERACTIVE                0x10    /* fn_flags */

#define ZEND_ACC_PUBLIC                    0x100    /* fn_flags, zend_property_info.flags */

#define ZEND_ACC_PROTECTED                  0x200    /* fn_flags, zend_property_info.flags */

#define ZEND_ACC_PRIVATE                    0x400    /* fn_flags, zend_property_info.flags */

#define ZEND_ACC_PPP_MASK  (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)

#define ZEND_ACC_CHANGED                    0x800    /* fn_flags, zend_property_info.flags */

#define ZEND_ACC_IMPLICIT_PUBLIC            0x1000  /* zend_property_info.flags; unused (1) */

#define ZEND_ACC_CTOR                      0x2000  /* fn_flags */     

#define ZEND_ACC_DTOR                      0x4000  /* fn_flags */   

#define ZEND_ACC_CLONE                      0x8000  /* fn_flags */

#define ZEND_ACC_ALLOW_STATIC              0x10000  /* fn_flags */

#define ZEND_ACC_SHADOW                    0x20000  /* fn_flags */

#define ZEND_ACC_DEPRECATED                0x40000  /* fn_flags */

#define ZEND_ACC_CLOSURE                    0x100000 /* fn_flags */

#define ZEND_ACC_CALL_VIA_HANDLER          0x200000 /* fn_flags */


三、申明和更新类中的属性

ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value TSRMLS_DC); 

ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length TSRMLS_DC); 

ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, long value TSRMLS_DC); 

ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value TSRMLS_DC); 

ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value TSRMLS_DC); 

ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length TSRMLS_DC); 

ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value TSRMLS_DC);

ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, char *name, int name_length TSRMLS_DC);

ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC);

ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC);

ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, char *name, int name_length, double value TSRMLS_DC);

ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, char *name, int name_length, const char *value TSRMLS_DC);

ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, char *name, int name_length, const char *value, int value_length TSRMLS_DC);

ZEND_API int zend_update_static_property_null(zend_class_entry *scope, char *name, int name_length TSRMLS_DC);

ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, char *name, int name_length, long value TSRMLS_DC);

ZEND_API int zend_update_static_property_long(zend_class_entry *scope, char *name, int name_length, long value TSRMLS_DC);

ZEND_API int zend_update_static_property_double(zend_class_entry *scope, char *name, int name_length, double value TSRMLS_DC);

ZEND_API int zend_update_static_property_string(zend_class_entry *scope, char *name, int name_length, const char *value TSRMLS_DC);

ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, char *name, int name_length, const char *value, int value_length TSRMLS_DC);


动态添加属性

#define add_property_long(__arg, __key, __n) add_property_long_ex(__arg, __key, strlen(__key)+1, __n TSRMLS_CC)

#define add_property_null(__arg, __key) add_property_null_ex(__arg, __key, strlen(__key) + 1 TSRMLS_CC)

#define add_property_bool(__arg, __key, __b) add_property_bool_ex(__arg, __key, strlen(__key)+1, __b TSRMLS_CC)

#define add_property_resource(__arg, __key, __r) add_property_resource_ex(__arg, __key, strlen(__key)+1, __r TSRMLS_CC)

#define add_property_double(__arg, __key, __d) add_property_double_ex(__arg, __key, strlen(__key)+1, __d TSRMLS_CC)

#define add_property_string(__arg, __key, __str, __duplicate) add_property_string_ex(__arg, __key, strlen(__key)+1, __str, __duplicate TSRMLS_CC)

#define add_property_stringl(__arg, __key, __str, __length, __duplicate) add_property_stringl_ex(__arg, __key, strlen(__key)+1, __str, __length, __duplicate TSRMLS_CC)

#define add_property_zval(__arg, __key, __value) add_property_zval_ex(__arg, __key, strlen(__key)+1, __value TSRMLS_CC)

四、一些其它的宏

#define INIT_CLASS_ENTRY(class_container, class_name, functions) INIT_OVERLOADED_CLASS_ENTRY(class_container, class_name, functions, NULL, NULL, NULL)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6435a43749282e4ff291fad89d79a04c.html