您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

原创:PHP内核研究之类的成员属性和方法

2024/6/15 17:29:02发布22次查看
这一篇要详细讲讲php类的成员属性及方法.
上一篇中曾经介绍到zend_do_begin_class_declaration这个函数,它用来创建并初始化一个zend_class_entry
类的所有信息都保存在这个结构中,那么 属性和方法是怎么保存的呢?
1
2
3
classperson{
      public$name;
}
还记得上一篇说过的zend_initialize_class_data函数吗?不记得也没关系.我们仔细来瞧瞧这个函数
zend_initialize_class_data(new_class_entry, 1 tsrmls_cc);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
zend_apivoidzend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers tsrmls_dc)/* {{{ */
{
        zend_bool persistent_hashes = (ce->type == zend_internal_class) ? 1 : 0;
        dtor_func_t zval_ptr_dtor_func = ((persistent_hashes) ? zval_internal_ptr_dtor : zval_ptr_dtor);
ce->refcount = 1;
        ce->constants_updated = 0;
        ce->ce_flags = 0;
ce->doc_comment = null;
        ce->doc_comment_len = 0;
zend_hash_init_ex(&ce->default_properties, 0, null, zval_ptr_dtor_func, persistent_hashes, 0);
        zend_hash_init_ex(&ce->properties_info, 0, null, (dtor_func_t) (persistent_hashes ? zend_destroy_property_info_internal : zend_destroy_property_info), persistent_hashes, 0);
        zend_hash_init_ex(&ce->default_static_members, 0, null, zval_ptr_dtor_func, persistent_hashes, 0);        zend_hash_init_ex(&ce->constants_table, 0, null, zval_ptr_dtor_func, persistent_hashes, 0);
        zend_hash_init_ex(&ce->function_table, 0, null, zend_function_dtor, persistent_hashes, 0);
if(ce->type == zend_internal_class) {
#ifdef zts
                intn = zend_hash_num_elements(cg(class_table));
if(cg(static_members) && n >= cg(last_static_member)) {
                        /* support for run-time declaration: dl() */
                        cg(last_static_member) = n+1;
                        cg(static_members) =realloc(cg(static_members), (n+1)*sizeof(hashtable*));
                        cg(static_members)[n] = null;
                }
                ce->static_members = (hashtable*)(zend_intptr_t)n;
#else
                ce->static_members = null;
#endif
       }else{
                ce->static_members = &ce->default_static_members;
        }
if(nullify_handlers) {
                ce->constructor = null;
                ce->destructor = null;
                ce->clone = null;
                ce->__get = null;
                ce->__set = null;
                ce->__unset = null;
                ce->__isset = null;
                ce->__call = null;
                ce->__callstatic = null;
                ce->__tostring = null;
                ce->create_object = null;
                ce->get_iterator = null;
                ce->iterator_funcs.funcs = null;
                ce->interface_gets_implemented = null;
                ce->get_static_method = null;
                ce->parent = null;
                ce->num_interfaces = 0;
                ce->interfaces = null;
                ce->module = null;
                ce->serialize = null;
                ce->unserialize = null;
                ce->serialize_func = null;
                ce->unserialize_func = null;
                ce->builtin_functions = null;
        }
}
zend_bool persistent_hashes = (ce->type == zend_internal_class) ? 1 : 0;
普通用户类与内部类 分配内存的方式不同….为什么会有区别呢?我还没来得及研究哦..^.^
注意看13-16行.
zend_hash_init_ex(&ce->default_properties, 0, null, zval_ptr_dtor_func, persistent_hashes, 0);
zend_hash_init_ex(&ce->default_static_members, 0, null, zval_ptr_dtor_func, persistent_hashes, 0);
zend_hash_init_ex(&ce->constants_table, 0, null, zval_ptr_dtor_func, persistent_hashes, 0);
zend_hash_init_ex(&ce->function_table, 0, null, zend_function_dtor, persistent_hashes, 0);
如果你看过之前的文章,那么你肯定知道这是在初始化hashtable.
是的..确实是这样,
default_properties,default_static_members等都是hashtable类型的指针.所以初始化当然要zend_hash_init了.
第36-61行初始化魔术方法
不过这里只是初始化哦..好像并没有设置属性.$name属性是如何添加到属性表里的呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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_statement:
                variable_modifiers { cg(access_type) = z_lval($1.u.constant); } class_variable_declaration';'
        |       class_constant_declaration';'
        |       method_modifiers function is_reference t_string { zend_do_begin_function_declaration(&$2, &$4, 1, $3.op_type, &$1 tsrmls_cc); }'('
                        parameter_list')'method_body { zend_do_abstract_method(&$4, &$1, &$9 tsrmls_cc); zend_do_end_function_declaration(&$2 tsrmls_cc); }
;
class_variable_declaration:
                class_variable_declaration','t_variable                                       { zend_do_declare_property(&$3, null, cg(access_type) tsrmls_cc); }
        |       class_variable_declaration','t_variable'='static_scalar     { zend_do_declare_property(&$3, &$5, cg(access_type) tsrmls_cc); }
        |       t_variable                                              { zend_do_declare_property(&$1, null, cg(access_type) tsrmls_cc); }
        |       t_variable'='static_scalar    { zend_do_declare_property(&$1, &$3, cg(access_type) tsrmls_cc); }
;
这个还记得吧?
类初始化成功后类里面的东西当然要执行class_statement_list这个啦..^.^
类体里会调用 zend_do_declare_property处理.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
voidzend_do_declare_property(constznode *var_name,constznode *value, zend_uint access_type tsrmls_dc)/* {{{ */
{
        zval *property;
        zend_property_info *existing_property_info;
        char*comment = null;
        intcomment_len = 0;
if(cg(active_class_entry)->ce_flags & zend_acc_interface) {
                zend_error(e_compile_error,interfaces may not include member variables);
        }
if(access_type & zend_acc_abstract) {
                zend_error(e_compile_error,properties cannot be declared abstract);
        }
if(access_type & zend_acc_final) {
                zend_error(e_compile_error,cannot declare property %s::$%s final, the final modifier is allowed only for methods and classes,
                                        cg(active_class_entry)->name, var_name->u.constant.value.str.val);
        }
if(zend_hash_find(&cg(active_class_entry)->properties_info, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, (void**) &existing_property_info)==success) {
                if(!(existing_property_info->flags & zend_acc_implicit_public)) {
                        zend_error(e_compile_error,cannot redeclare %s::$%s, cg(active_class_entry)->name, var_name->u.constant.value.str.val);
                }
        }
        alloc_zval(property);
if(value) {
                *property = value->u.constant;
        }else{
                init_pzval(property);
                z_type_p(property) = is_null;
        }
if(cg(doc_comment)) {
                comment = cg(doc_comment);
                comment_len = cg(doc_comment_len);
                cg(doc_comment) = null;
                cg(doc_comment_len) = 0;
        }
zend_declare_property_ex(cg(active_class_entry), var_name->u.constant.value.str.val, var_name->u.constant.value.str.len, property, access_type, comment, comment_len tsrmls_cc);
        efree(var_name->u.constant.value.str.val);
}
第8-25行:
如果你的类声明的是接口.那么该接口是不能有属性的 会抛出interfaces may not include member variables
如果类的属性被设置为abstract,那么会抛出properties cannot be declared abstract
如果类的属性被设置为final,那么会抛出cannot declare property %s::$%s final, the final modifier is allowed only for methods and classes
一切没有问题,会分配一个zval的数据,
如果属性有初始值,那么该数据会分配给zval,如果没有,则调用init_pzval初始化zval,并设置类型为is_null;
最后会调用zend_declare_property_ex将该zval添加到指定的active_class_entry中
类的方法
1
2
3
4
5
classperson{
      publicfunctiontest(){
             echo1;
      }
}
如果是方法呢是怎么处理的?
先看规则
1
2
3
4
5
class_statement:
                variable_modifiers { cg(access_type) = z_lval($1.u.constant); } class_variable_declaration';'
        |       class_constant_declaration';'
        |       method_modifiers function is_reference t_string { zend_do_begin_function_declaration(&$2, &$4, 1, $3.op_type, &$1 tsrmls_cc); }'('
                        parameter_list')'method_body { zend_do_abstract_method(&$4, &$1, &$9 tsrmls_cc); zend_do_end_function_declaration(&$2 tsrmls_cc); }
第一个是属性,那么第三个就是就是方法啦..
zend_do_begin_function_declaration眼熟吗?
如果看过之前的文章,肯定眼熟
如果没有看过.先去看看这篇文章. 函数的定义
这里就不详细讲了.
只说说在那篇没提到的内容
在这个函数中 有一个判断
1
2
3
4
5
6
7
8
9
10
11
if(is_method) {
                if(cg(active_class_entry)->ce_flags & zend_acc_interface) {
                        if((z_lval(fn_flags_znode->u.constant) & ~(zend_acc_static|zend_acc_public))) {
                                zend_error(e_compile_error,access type for interface method %s::%s() must be omitted, cg(active_class_entry)->name, function_name->u.constant.value.str.val);
                        }
                        z_lval(fn_flags_znode->u.constant) |= zend_acc_abstract;/* propagates to the rest of the parser */
                }
                fn_flags = z_lval(fn_flags_znode->u.constant);/* must be done *after* the above check */
        }else{
                fn_flags = 0;
        }
很明显,如果是方法 ,那么才会进去处理
3-5行 :
如果你把接口类的属性设置为private私有或受保护的.那么就会抛出access type for interface method %s::%s() must be omitted
然后会调用
if (zend_hash_add(&cg(active_class_entry)->function_table, lcname, name_len+1, &op_array, sizeof(zend_op_array), (void **) &cg(active_op_array)) == failure) {
zend_error(e_compile_error, “cannot redeclare %s::%s()”, cg(active_class_entry)->name, name);
}
直接把方法添加到function_table里.
下面会根据不同的类声明做不同的判断.
 以上就是原创:php内核研究之类的成员属性和方法的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product