PHP內核研究之類的成員屬性和方法

聲明:本文爲斯人原創,全部爲作者一一分析得之,有不對的地方望賜教。

博客地址:PHP技術博客 在CSDN也會同步更新的哦.

歡迎轉載,轉載請註明出處


上一章講到類的實現

這一篇要詳細講講PHP類的成員屬性及方法.
上一篇中曾經介紹到zend_do_begin_class_declaration這個函數,它用來創建並初始化一個zend_class_entry
類的所有信息都保存在這個結構中,那麼 屬性和方法是怎麼保存的呢?
class Person{
      public $name;
}




還記得上一篇說過的zend_initialize_class_data函數嗎?不記得也沒關係.我們仔細來瞧瞧這個函數
zend_initialize_class_data(new_class_entry, 1 TSRMLS_CC);<!--more-->
ZEND_API void zend_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
                int n = 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屬性是如何添加到屬性表裏的呢???
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處理.
void zend_do_declare_property(const znode *var_name, const znode *value, zend_uint access_type TSRMLS_DC) /* {{{ */
{
        zval *property;
        zend_property_info *existing_property_info;
        char *comment = NULL;
        int comment_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中
<strong>類的方法</strong>
[php]
class Person{
      public function test(){
             echo 1;
      }
}
[/php]
如果是方法呢??是怎麼處理的?
先看規則
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眼熟嗎?
如果看過之前的文章,肯定眼熟
如果沒有看過.先去看看這篇文章.<a href="http://imsiren.com/archives/295"> 函數的定義</a>
這裏就不詳細講了.
只說說在那篇沒提到的內容
在這個函數中 有一個判斷
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內核研究之類的成員屬性和方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章