1.PHP中类编译阶段的产物,而对象是运行时产生的,它们归属于不同阶段。一个类可以包含有属于自己的常量、变量(称为“属性”)以及函数(称为“方法”)。
2.首先我们看下类的数据结构:
struct_zend_class_entry {char type; //类的类型:内部类ZEND_INTERNAL_CLASS(1)、用户自定义类ZEND_USER_CLASS(2)
zend_string *name; //类名,PHP类不区分大小写,统一为小写
struct _zend_class_entry *parent; //父类
intrefcount;
uint32_t ce_flags;//类掩码,如普通类、抽象类、接口,除了这还有别的含义,暂未弄清
int default_properties_count; //普通属性数,包括public、private
int default_static_members_count; //静态属性数,static
zval *default_properties_table; //普通属性值数组
zval *default_static_members_table; //静态属性值数组
zval *static_members_table;
HashTable function_table;//成员方法哈希表
HashTable properties_info; //成员属性基本信息哈希表,key为成员名,value为zend_property_info
HashTable constants_table; //常量哈希表,通过constant定义的//以下是构造函授、析构函数、魔术方法的指针
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*__callstatic;
union _zend_function*__tostring;
union _zend_function*__debugInfo;
union _zend_function*serialize_func;
union _zend_function*unserialize_func;
zend_class_iterator_funcs iterator_funcs;//自定义的钩子函数,通常是定义内部类时使用,可以灵活的进行一些个性化的操作//用户自定义类不会用到,暂时忽略即可
zend_object* (*create_object)(zend_class_entry *class_type);
zend_object_iterator*(*get_iterator)(zend_class_entry *ce, zval *object, intby_ref);int (*interface_gets_implemented)(zend_class_entry *iface, zend_class_entry *class_type); /*a class implements this interface*/union _zend_function*(*get_static_method)(zend_class_entry *ce, zend_string*method);/*serializer callbacks*/
int (*serialize)(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data);int (*unserialize)(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data);
uint32_t num_interfaces;//实现的接口数
uint32_t num_traits;
zend_class_entry**interfaces; //实现的接口
zend_class_entry**traits;
zend_trait_alias**trait_aliases;
zend_trait_precedence**trait_precedences;
union {struct{
zend_string*filename;
uint32_t line_start;
uint32_t line_end;
zend_string*doc_comment;
} user;struct{const struct _zend_function_entry *builtin_functions;struct _zend_module_entry *module; //所属扩展
} internal;
} info;
}
create_object为实例化对象的操作,可以通过扩展自定义一个函数来接管实例化对象的操作,没有定义这个函数的话将由默认的zend_objects_new()处理,自定义时可以参考这个函数的实现:
//注意:此操作并没有将属性拷贝到zend_object中:由object_properties_init()完成
ZEND_API zend_object *zend_objects_new(zend_class_entry *ce)
{
zend_object*object = emalloc(sizeof(zend_object) +zend_object_properties_size(ce));
zend_object_std_init(object, ce);//设置对象操作的handler
object->handlers = &std_object_handlers;return object;
}
举个例子具体看下,定义一个User类,它继承了Human类,User类中有一个常量、一个静态属性、两个普通属性:
//父类
classHuman {}classUser extends Human
{const type = 110;static $name = "uuu";public $uid = 900;public $sex = ‘w‘;publicfunction __construct(){
}publicfunction getName(){return $this->name;
}
}
其对应的zend_class_entry存储结构如下图
开始的时候已经提到,类是编译阶段的产物,编译完成后我们定义的每个类都会生成一个zend_class_entry,它保存着类的全部信息,在执行阶段所有类相关的操作都是用的这个结构。
PHP脚本中定义的类以及内核、扩展中定义的内部类通过一个以"类名"作为索引的哈希表存储,EG(class_table)。
常量 常量是类维度的数据(而不是对象的),通过zend_class_entry.constants_table进行存储,这是一个哈希结构,通过 常量名 索引,value就是具体定义的常量值。常量名保存在literals中(其op_type = IS_CONST),执行时先取出常量名,然后去zend_class_entry.constants_table哈希表中索引到具体的常量值即可。
成员属性 员属性又分为两类:普通属性、静态属性。静态属性通过 static 声明,通过 self::$property 或 类名::$property 访问;普通属性通过 $this->property 或 $object->property 访问。
成员属性的 初始化值 并不是 直接 用以"属性名"作为索引的哈希表存储的,而是通过数组保存的,普通属性、静态属性各有一个数组分别存储
访问时仍然是根据以"属性名"为索引的散列表查找具体VALUE的,这个散列表并没有按照普通属性、静态属性分为两个,而是只用了一个:HashTable properties_info 。此哈希表存储元素的value类型为 zend_property_info 。
typedef struct_zend_property_info {
uint32_t offset;//普通成员变量的内存偏移值//静态成员变量的数组索引
uint32_t flags; //属性掩码,如public、private、protected及是否为静态属性
zend_string *name; //属性名:并不是原始属性名
zend_string *doc_comment;
zend_class_entry*ce; //所属类
} zend_property_info;//flags标识位
#define ZEND_ACC_PUBLIC 0x100
#define ZEND_ACC_PROTECTED 0x200
#define ZEND_ACC_PRIVATE 0x400
#define ZEND_ACC_STATIC 0x01
成员方法 成员方法也有静态、非静态之分,静态方法中不能使用$this,因为其操作的作用域全部都是类的而不是对象的,而非静态方法中可以通过$this访问属于本对象的成员属性。
类的编译过程,主要围绕zend_class_entry进行的操作,类插入EG(class_table)的过程相当于类的声明在编译阶段提前"执行"了,也有可能因为父类找不到等原因延至运行时执行
内部类的实现就是创建一个zend_class_entry结构,然后插入到EG(class_table)中。