首页 > 后端开发 > php教程 > 原创:PHP内核研究之类的实现

原创:PHP内核研究之类的实现

黄舟
发布: 2023-03-03 21:04:02
原创
1206 人浏览过

这几天比较忙哦..没有时间写..周末了多写几篇吧.
目前大部分语言都支持类.
类是什么?类就是面向对象,简称OOP.英文名字 Object Oriented Programming.
面向对象是什么?是一种编程架构.
OOP的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成,OOP达到了软件工程的三个目标:重用性、灵活性和扩展性.
因为我们讲的不是这里只简单描述,如果你还不知道什么是类,什么是面向对象..那么这篇文章目前不适合你哦.

classPerson{

  

};

   

上面是创建一个PHP类.class是PHP的关键字.通过它我们就能找到Zend是如何创建类的.


unticked_class_declaration_statement:

                class_entry_type T_STRING extends_from

                        { zend_do_begin_class_declaration(&$1, &$2, &$3 TSRMLS_CC); }

                        implements_list

                        '{'

                                class_statement_list

                        '}'{ zend_do_end_class_declaration(&$1, &$2 TSRMLS_CC); }

        |       interface_entry T_STRING

                        { zend_do_begin_class_declaration(&$1, &$2, NULL TSRMLS_CC); }

                        interface_extends_list

                        '{'

                                class_statement_list

                        '}'{ zend_do_end_class_declaration(&$1, &$2 TSRMLS_CC); }

;

class_entry_type:

                T_CLASS                 { $$.u.opline_num = CG(zend_lineno); $$.u.EA.type = 0; }

        |       T_ABSTRACT T_CLASS { $$.u.opline_num = CG(zend_lineno); $$.u.EA.type = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; }

        |       T_FINAL T_CLASS { $$.u.opline_num = CG(zend_lineno); $$.u.EA.type = ZEND_ACC_FINAL_CLASS; }

;

   

T_CLASS,T_ABSTRACT T_CLASS和T_FINAL 是PHP的三种类的模式
T_CLASS:是一个标准类.
T_ABSTRACT:是声明一个抽象类
T_FINAL:声明一个不容许继承和扩展的类.
当然还有interface
他们定义在Zend/zend_complie.h的文件中

   

#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS    0x10    //没有声明为抽象,但是内部有抽象方法

#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS    0x20   //抽象

#define ZEND_ACC_FINAL_CLASS                0x40  //Final

#define ZEND_ACC_INTERFACE                  0x80 //接口

   

这三个规则 记录当前行,并设置类的类型.
在定义类的时候调用了 zend_do_begin_class_declaration和zend_do_end_class_declaration两个方法,
类的关键字 ,类的名称和所继承的父类作为参数传递给这两个函数.
zend_do_begin_class_declaration是用来声明类,设置类型,创建一个
zend_do_end_class_declaration用来处理类中的属性及方法.
在讲到两个函数之前一定先要说说 保存类的结构zend_class_entry
它定义在Zend/zend.h中


struct_zend_class_entry {

        chartype;

        char*name;//类名称

        zend_uint name_length;

        struct_zend_class_entry *parent;//所继承的父类

        intrefcount; //引用数

        zend_bool constants_updated;//类的类型

        zend_uint ce_flags;//类的类型 抽象?接口?Final?

        HashTable function_table; //函数表

        HashTable default_properties;//属性

        HashTable properties_info; //函数的访问级别

        HashTable default_static_members;//静态成员

        HashTable *static_members;//静态成员,当是用户声明的类等于default_static_members,内置的类为NULL

        HashTable constants_table;

        conststruct_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 *__callstatic;

        union_zend_function *__tostring;

        union_zend_function *serialize_func;

        union_zend_function *unserialize_func;

  

        zend_class_iterator_funcs iterator_funcs;

  

        /* handlers */

        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);/* 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);

  

        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;

};

   

清楚了这个结构之后 下面来看看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中文网(m.sbmmt.com)!


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板