Original: Member attributes and methods such as PHP kernel research

Source: Internet
Author: User
Abstract: Original: PHP kernel research and other member attributes and methods... this article will detail the member attributes and methods of the PHP class.
The previous article introduced zend_do_begin_class_declaration, which is used to create and initialize a zend_class_entry
All information of the class is saved in this structure. how are attributes and methods saved?


1

2

3

ClassPerson {

Public $ name;

}

Do you still remember the zend_initialize_class_data function mentioned in the previous article? It doesn't matter if you don't remember. let's take a closer look at this function.
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 );

Values (& ce-> default_static_members, 0, NULL, values, persistent_hashes, 0); zend_hash_init_ex (& ce-> constants_table, 0, NULL, values, 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;
The memory allocation method for common user classes is different from that for internal classes .... Why is there a difference ??? I haven't studied it yet... ^. ^
Check Lines 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 );
If you have read the previous article, you must know that it is initializing HashTable.
Yes .. it is,
Default_properties and default_static_members are all HashTable pointers. Therefore, zend_hash_init is required for initialization.
Line 36-61 initialize magic methods
However, this is only initialization. it seems that the attribute. $ name is not set. how is the attribute added to the attribute table ???

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_effecact_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 );}

;

Remember this?
After the class initialization is successful, you must execute class_statement_list... ^. ^.
The class body calls zend_do_declare_property for processing.

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

}

Line 8-25:
If your class declares an interface, Interfaces may not include member variables will be thrown if this interface does not have attributes.
If the attribute of the class is set to abstract, Properties cannot be declared abstract will be thrown.
If the class property is set to final, Cannot declare property % s: $ % s final, the final modifier is allowed only for methods and classes will be thrown.
If everything is okay, a zval data will be allocated,
If the attribute has an initial value, the data is allocated to zval. If no value exists, INIT_PZVAL is called to initialize zval and IS_NULL is set;
Zend_declare_property_ex will be called to add the zval to the specified active_class_entry.
Class method

1

2

3

4

5

ClassPerson {

Publicfunctiontest (){

Echo1;

}

}

What if it is a method ?? How is this handled?
First View rules

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_effecact_method (& $4, & $1, & $9 TSRMLS_CC); zend_do_end_function_declaration (& $2 TSRMLS_CC );}

The first is the attribute, and the third is the method ..
Is zend_do_begin_function_declaration familiar?
If you have read the previous articles, you must be familiar with them.
If you have not read it, read this article first. function definition
I will not go into detail here.
Let's just talk about the content not mentioned in that article.
There is a judgment in this function

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;

}

Obviously, if it is a method, it will be processed.
3-5 rows:
If you set the attribute of the interface class to private or protected, the Access type for interface method % s: % s () must be omitted will be thrown.
Then
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 );
}
Directly add the method to function_table.
Next we will make different judgments based on different class declarations.

The above is original: PHP kernel research and other member attributes and methods. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.