PHP7 Kernel Analysis 8 and the like

不言
Release: 2023-03-23 18:10:01
Original
1582 people have browsed it

The content of this article is about PHP7 kernel analysis 8 and so on. Now I share it with you. Friends in need can refer to it

1. Class structure

Class is the product of the compilation phase. After compilation, each class we define will generate a zend_class_entry, which stores all the information of the class. During the execution phase, all class-related operations use this structure
struct _zend_class_entry {
    char type;          //类的类型:内部类ZEND_INTERNAL_CLASS(1)、用户自定义类ZEND_USER_CLASS(2)
    zend_string *name;  //类名,PHP类不区分大小写,统一为小写
    struct _zend_class_entry *parent; //父类
    int refcount;
    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;
}
Copy after login


2. Class constants

In PHP, values ​​that always remain unchanged in the class can be defined as constants. When defining and using There is no need to use the $ symbol when using constants. The value of a constant must be a fixed value. They are stored through zend_class_entry.constants_table, which is a hash structure
常量的读取:

class my_class {
    const A1 = "hi";
}
echo my_class::A1;

编译到echo my_class::A1这行时首先会尝试检索下是否已经编译了my_class,如果能在CG(class_table)中找到,则进一步从类的contants_table查找对应的常量,找到的话则会复制其value替换常量,简单的讲就是类似C语言中的宏,编译时替换为实际的值了,而不是在运行时再去检索。

echo my_class::A1;

class my_class {
    const A1 = "hi";
}

在运行时再去检索。替换成为实际的值
Copy after login


3. Member attributes

The variables in the attributes can be initialized, but the initialized value must be a constant. The constant here means that the PHP script can get its value during the compilation phase, and does not depend on Only runtime information can be evaluated, such as public $time = time(); defining a property in this way will trigger a syntax error.

Member attributes are divided into two categories: ordinary attributes and static attributes. Different from the storage method of constants, the initialization value of member attributes is not directly stored in the hash table with "attribute name" as the index, but through The

PHP7 Kernel Analysis 8 and the like

# saved in the array is actually just the VALUE of the member attribute stored in the array. When accessed, it is still based on the "attribute name" as the index. The hash table looks for a specific VALUE, and this hash table is zend_class_entry.properties_info
typedef struct _zend_property_info {
    uint32_t offset; //普通成员变量的内存偏移值,静态成员变量的数组索引
    uint32_t flags;  //属性掩码,如public、private、protected及是否为静态属性
    zend_string *name; //属性名:并不是原始属性名,private会在原始属性名前加上类名,protected则会加上*作为前缀
    zend_string *doc_comment;
    zend_class_entry *ce; //所属类
} zend_property_info;
Copy after login


##4. Member method

Each A class can define several functions (called member methods) that belong to this class. These functions are the same as ordinary functions, but are managed in the class dimension and are not global, so the member methods are stored in the class instead of EG ( function_table)

PHP7 Kernel Analysis 8 and the like

Member methods are also divided into static and non-static. $this cannot be used in static methods because the scope of their operations is all It is a class rather than an object. In a non-static method, you can access the member attributes belonging to this object through $this


5. Object data structure

typedef struct _zend_object     zend_object;

struct _zend_object {
    zend_refcounted_h gc; //引用计数
    uint32_t          handle; //对象编号
    zend_class_entry *ce; //所属类
    const zend_object_handlers *handlers; //对象操作处理函数
    HashTable        *properties; //普通成员属性哈希表
    zval              properties_table[1]; //普通属性值数组
};
Copy after login
Creation of objects: First, search the corresponding zend_class_entry in EG (class_table) according to the class name, then create and initialize an object, and finally initialize the zend_execute_data that calls the constructor
Related recommendations :


PHP7 Kernel Analysis 7 Zend Engine Execution Process

PHP7 Kernel Analysis 6 Function

PHP7 Kernel Analysis 5 Compilation of PHP Code

The above is the detailed content of PHP7 Kernel Analysis 8 and the like. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!