这一篇要详细讲讲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内核研究之类的成员属性和方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中