设为首页 收藏本站
查看: 561|回复: 0

[经验分享] PHP内核研究之类的成员属性和方法

[复制链接]

尚未签到

发表于 2017-4-10 10:22:17 | 显示全部楼层 |阅读模式

声明:本文为斯人原创,全部为作者一一分析得之,有不对的地方望赐教。

博客地址: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 ZTSint 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;#elsece->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内核研究之类的成员属性和方法

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362781-1-1.html 上篇帖子: 在PHP中利用XML技术构造远程服务(上) 下篇帖子: MongoDB的MapReduce用法及php示例代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表