Home Backend Development PHP Tutorial Original: PHP kernel research and other implementations

Original: PHP kernel research and other implementations

Dec 22, 2016 am 09:56 AM

I’ve been quite busy these days. I don’t have time to write. I’ll write a few more articles during the weekend.
Most languages ​​currently support classes.
What are classes? Classes are object-oriented, or OOP for short. The English name is Object Oriented Programming.
What is object-oriented? It is a programming architecture.
A basic principle of OOP is that a computer program is composed of a single unit or object that can function as a subroutine. OOP achieves three goals of software engineering: Reusability , flexibility and scalability.
Because what we are talking about is not just a simple description here. If you don’t know what a class is and what is object-oriented... then this article is not suitable for you at the moment.

classPerson{

};

The above is to create a PHP class. class is the keyword of PHP. Through it we can find out how Zend creates the class. class_entry_type T_STRING extends_from

                                                                                                            ​                                                                                                                                              LS_CC); }

    | interface_entry T_STRING​​​                                                                                                                                TSRMLS_CC); }

;

class_entry_type :

                                                                                                             CG(zend_lineno); $$. u.EA.type = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; }

|

T_CLASS, T_ABSTRACT T_CLASS and T_FINAL are the three modes of PHP

T_CLASS: It is a standard class.

T_ABSTRACT: It declares an abstract class

T_FINAL: It declares a class that does not allow inheritance and extension.

Of course there is also interface

They are defined in Zend In the file /zend_complie.h

#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS 0x10 //Not declared as abstract, but there are abstract methods inside

#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS 0x20 //Abstract

#define ZEND _ACC_FINAL_CLASS 0x40 //Final

#define ZEND_ACC_INTERFACE 0x80 //Interface

These three rules record the current line and set the type of the class.
When defining the class, the two methods zend_do_begin_class_declaration and zend_do_end_class_declaration are called.
The keyword of the class, the name of the class and the inherited parent class are passed as parameters to this Two functions.
zend_do_begin_class_declaration is used to declare a class, set the type, and create a
zend_do_end_class_declaration to process the attributes and methods in the class.
Before talking about the two functions, we must first talk about the structure of the saved class zend_class_entry
It defines In Zend/zend.h


struct_zend_class_entry {

                                                                    struct_zend_class_entry *parent;//Inherited parent class

intrefcount; //Number of references

              zend_bool constants_updated                                    HashTable default_properties;//Properties

B Hashtable Properties_info; // The access level of the function

hashtable default_Static_members; // Static members

havetable *static_members; _Static_members, the built -in class is null

hashtable constants_table;

C Construct_zend_function_ENTRY *Builtin_functions;

// Is it familiar ??? The magic function is here ...

union_function *constructor; UCTOR; n 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;

                                                                           with with with with with with with, with zend_class_iterator_funcs iterator_funcs;

                                                                                        ’ (*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);/* a class implements this interface */

      union_zend_function *(*get_static_method)(zend_class_entry *ce,char* method,intmethod_len TSRMLS_DC);

/ * serializer callbacks */

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); char*filename; //Declare the file address of the class

        zend_uint line_start; // Class start Line

            zend_uint line_end; // Class end line

        char*doc_comment;

                                                    ‐                         

  

        struct_zend_module_entry *module;

};

   

清楚了这个结构之后 下面来看看zend_do_begin_class_declaration函数

   

voidzend_do_begin_class_declaration(constznode *class_token, znode *class_name,constznode *parent_class_name TSRMLS_DC)/* {{{ */

{

        zend_op *opline;

        intdoing_inheritance = 0;

        zend_class_entry *new_class_entry;

        char*lcname;

        interror = 0;

        zval **ns_name;

  

        if(CG(active_class_entry)) {

                zend_error(E_COMPILE_ERROR,"Class declarations may not be nested");

                return;

        }

  

        lcname = zend_str_tolower_dup(class_name->u.constant.value.str.val, class_name->u.constant.value.str.len);

  

        if(!(strcmp(lcname,"self") &&strcmp(lcname,"parent"))) {

                efree(lcname);

                zend_error(E_COMPILE_ERROR,"Cannot use '%s' as class name as it is reserved", class_name->u.constant.value.str.val);

        }

  

        /* Class name must not conflict with import names */

        if(CG(current_import) &&

                        zend_hash_find(CG(current_import), lcname, Z_STRLEN(class_name->u.constant)+1, (void**)&ns_name) == SUCCESS) {

                error = 1;

        }

       if(CG(current_namespace)) {

                /* Prefix class name with name of current namespace */

                znode tmp;

  

                tmp.u.constant = *CG(current_namespace);

                zval_copy_ctor(&tmp.u.constant);

                zend_do_build_namespace_name(&tmp, &tmp, class_name TSRMLS_CC);

                class_name = &tmp;

                efree(lcname);

                lcname = zend_str_tolower_dup(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant));

        }

  

        if(error) {

                char*tmp = zend_str_tolower_dup(Z_STRVAL_PP(ns_name), Z_STRLEN_PP(ns_name));

  

                if(Z_STRLEN_PP(ns_name) != Z_STRLEN(class_name->u.constant) ||

                        memcmp(tmp, lcname, Z_STRLEN(class_name->u.constant))) {

                        zend_error(E_COMPILE_ERROR,"Cannot declare class %s because the name is already in use", Z_STRVAL(class_name->u.constant));

                }

                efree(tmp);

        }

  

        new_class_entry = emalloc(sizeof(zend_class_entry));

        new_class_entry->type = ZEND_USER_CLASS;

        new_class_entry->name = class_name->u.constant.value.str.val;

        new_class_entry->name_length = class_name->u.constant.value.str.len;

  

        zend_initialize_class_data(new_class_entry, 1 TSRMLS_CC);

        new_class_entry->filename = zend_get_compiled_filename(TSRMLS_C);

        new_class_entry->line_start = class_token->u.opline_num;

        new_class_entry->ce_flags |= class_token->u.EA.type;

if(parent_class_name && parent_class_name->op_type != IS_UNUSED) {

                switch(parent_class_name->u.EA.type) {

                        caseZEND_FETCH_CLASS_SELF:

                                zend_error(E_COMPILE_ERROR,"Cannot use 'self' as class name as it is reserved");

                                break;

                        caseZEND_FETCH_CLASS_PARENT:

                                zend_error(E_COMPILE_ERROR,"Cannot use 'parent' as class name as it is reserved");

                                break;

                        caseZEND_FETCH_CLASS_STATIC:

                                zend_error(E_COMPILE_ERROR,"Cannot use 'static' as class name as it is reserved");

                                break;

                        default:

                                break;

                }

                doing_inheritance = 1;

        }

  

        opline = get_next_op(CG(active_op_array) TSRMLS_CC);

        opline->op1.op_type = IS_CONST;

        build_runtime_defined_function_key(&opline->op1.u.constant, lcname, new_class_entry->name_length TSRMLS_CC);

  

        opline->op2.op_type = IS_CONST;

        opline->op2.u.constant.type = IS_STRING;

        Z_SET_REFCOUNT(opline->op2.u.constant, 1);

  

        if(doing_inheritance) {

                opline->extended_value = parent_class_name->u.var;

                opline->opcode = ZEND_DECLARE_INHERITED_CLASS;

        }else{

                opline->opcode = ZEND_DECLARE_CLASS;

        }

opline->op2.u.constant.value.str.val = lcname;

        opline->op2.u.constant.value.str.len = new_class_entry->name_length;

  

        zend_hash_update(CG(class_table), opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, &new_class_entry,sizeof(zend_class_entry *), NULL);

        CG(active_class_entry) = new_class_entry;

  

        opline->result.u.var = get_temporary_variable(CG(active_op_array));

        opline->result.op_type = IS_VAR;

        CG(implementing_class) = opline->result;

  

        if(CG(doc_comment)) {

                CG(active_class_entry)->doc_comment = CG(doc_comment);

                CG(active_class_entry)->doc_comment_len = CG(doc_comment_len);

                CG(doc_comment) = NULL;

                CG(doc_comment_len) = 0;

        }

}

   

lcname = zend_str_tolower_dup(class_name->u.constant.value.str.val, class_name->u.constant.value.str.len);
把所有类全部转换为小写处理.这就是为什么PHP大小写不敏感的原因.
if (!(strcmp(lcname, “self”) && strcmp(lcname, “parent”))) {
efree(lcname);
zend_error(E_COMPILE_ERROR, “Cannot use ‘%s’ as class name as it is reserved”, class_name->u.constant.value.str.val);
}
类的名字不能是self和parent.
第23-26行 用来检测类名是否重复定义.
第27-37行 用来设置命名空间,这是PHP5.3的新特性
第39-47行 用来抛出重复定义的错误
第49-57行 初始化保存类的结构
zend_initialize_class_data(new_class_entry, 1 TSRMLS_CC);函数是用来初始化结构里面的HashTable,魔术方法.
这个函数里面也有上面提到( HashTable *static_members; //静态成员,当是用户声明的类等于default_static_members,内置的类为NULL)的原因
第58-73行 同样用来检测父类的类名是否包含 保留关键字 self,parent,static
剩下的就是用来生成一个OP,
是内部类:那么生成的OP中间代码就是 ZEND_DECLARE_INHERITED_CLASS
是用户类:OP中间代码就是ZEND_DECLARE_CLASS
在这之后..Zend引擎会调用zend_execute函数执行OP的中间代码ZEND_DECLARE_CLASS_SPEC_HANDLER
它定义在Zend/zend_vm_execute.h中.
这个函数将执行关键代码
EX_T(opline->result.u.var).class_entry = do_bind_class(opline, EG(class_table), 0 TSRMLS_CC) ;
do_bind_class会将此类放到class_table中.当然 ,在这个函数里还会判断该类是否存在.不存在会抛出错误
Internal Zend error – Missing class information for %s
如果存在 则会添加成功
那么到这里类就创建成功了.
下一张节就要深入到 类内部了哦…

 以上就是原创:PHP内核研究之类的实现的内容,更多相关内容请关注PHP中文网(www.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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles