Home Backend Development PHP Tutorial Original: PHP kernel research such member attributes and methods

Original: PHP kernel research such member attributes and methods

Dec 22, 2016 am 09:58 AM

This article will talk about the member attributes and methods of PHP classes in detail.
In the previous article, we introduced the zend_do_begin_class_declaration function, which is used to create and initialize a zend_class_entry
All the information of the class is saved in this structure, then the attributes How are the methods and methods saved?


1

2

3

I still remember what I said in the previous article zend_initialize_class_data function? 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);

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

5 3

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-&g t;refcount = 1;

ce->constants_updated = 0;

ce-> ;ce_flags = 0;

ce->doc_comment = NULL; ce->doc_comment_len = 0;

zend_hash_init_ex(&ce-&g t;default_properties, 0, NULL, zval_ptr_dtor_func, persistent_hashes, 0); Zen d_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); ;

if(ce-> ;type == ZEND_INTERNAL_CLASS) {

#ifdef ZTS

                        intn = zend_hash_num_elements(CG(class_table));                                       t;= CG(last_static_member)) {

                                                                                                                            for run-time declaration: dl() */

                  CG(last_static_member)                                                                                                         );

                     CG(static_members)[n] = NULL;

#else

      ce->static_members = NULL;

#endif

                                                                                                                                                  

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

}

   

Lines 8-25:
If your class declares an interface, then the interface cannot have attributes and Interfaces may not include member variables will be thrown.
If the attributes of the class are set to abstract, then Properties cannot be thrown. be declared abstract
If the property of the class is set to final, then Cannot declare property %s::$%s final will be thrown, the final modifier is allowed only for methods and classes
Everything is fine, a zval data will be allocated ,
If the attribute has an initial value, then the data will be assigned to the zval. If not, INIT_PZVAL is called to initialize the zval and the type is set to IS_NULL;
Finally, zend_declare_property_ex will be called to add the zval to the specified active_class_entry
Method of the class

1

2

3

4

5

classPerson{

public functiontest(){

echo1;

       }

}

   

What if it’s the method?? How to deal with it?
Look at the rules first

1

2

3

4

5

class_statement:

                             (access_type) = Z_LVAL($1.u.constant); } class_variable_declaration';'

| RMLS_CC); }'('

                                                                                                                                                                                           { zend_do_abstract_method(&$4, &$1, &$9 TSRMLS_CC); zend_do_end_function_declaration(&$2 TSRMLS_CC); }

The first one is the attribute, then the third one is the method...

zend_do_begin_function_declaration Does it look familiar?

If you have read the previous article, you will definitely look familiar
If you haven’t read it, go read this article first. The definition of function
I won’t go into details here.
I will only talk about the content that was not mentioned in that article
In this function There is a judgment in

1

2

3

4

5

6

7

8

9

10

11

if(is_method) {

if(CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) {

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

                                         (fn_flags_znode->u .constant) |= ZEND_ACC_ABSTRACT;/* propagates to the rest of the parser */

                                                                                                                               */

                                                                                                                           The properties of the class are set to private or Protected. Then Access type for interface method %s::%s() must be omitted will be thrown and then called

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

}

Add the method directly to the function_table.

The following will make different judgments based on different class declarations.

The above is the original content: PHP kernel research and other member attributes and methods. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete WeChat friends? How to delete WeChat friends How to delete WeChat friends? How to delete WeChat friends Mar 04, 2024 am 11:10 AM

WeChat is one of the mainstream chat tools. We can meet new friends, contact old friends and maintain the friendship between friends through WeChat. Just as there is no such thing as a banquet that never ends, disagreements will inevitably occur when people get along with each other. When a person extremely affects your mood, or you find that your views are inconsistent when you get along, and you can no longer communicate, then we may need to delete WeChat friends. How to delete WeChat friends? The first step to delete WeChat friends: tap [Address Book] on the main WeChat interface; the second step: click on the friend you want to delete and enter [Details]; the third step: click [...] in the upper right corner; Step 4: Click [Delete] below; Step 5: After understanding the page prompts, click [Delete Contact]; Warm

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

Colorful motherboards enjoy high popularity and market share in the Chinese domestic market, but some users of Colorful motherboards still don’t know how to enter the bios for settings? In response to this situation, the editor has specially brought you two methods to enter the colorful motherboard bios. Come and try it! Method 1: Use the U disk startup shortcut key to directly enter the U disk installation system. The shortcut key for the Colorful motherboard to start the U disk with one click is ESC or F11. First, use Black Shark Installation Master to create a Black Shark U disk boot disk, and then turn on the computer. When you see the startup screen, continuously press the ESC or F11 key on the keyboard to enter a window for sequential selection of startup items. Move the cursor to the place where "USB" is displayed, and then

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Summary of methods to obtain administrator rights in Win11 Summary of methods to obtain administrator rights in Win11 Mar 09, 2024 am 08:45 AM

A summary of how to obtain Win11 administrator rights. In the Windows 11 operating system, administrator rights are one of the very important permissions that allow users to perform various operations on the system. Sometimes, we may need to obtain administrator rights to complete some operations, such as installing software, modifying system settings, etc. The following summarizes some methods for obtaining Win11 administrator rights, I hope it can help you. 1. Use shortcut keys. In Windows 11 system, you can quickly open the command prompt through shortcut keys.

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Detailed explanation of Oracle version query method Detailed explanation of Oracle version query method Mar 07, 2024 pm 09:21 PM

Detailed explanation of Oracle version query method Oracle is one of the most popular relational database management systems in the world. It provides rich functions and powerful performance and is widely used in enterprises. In the process of database management and development, it is very important to understand the version of the Oracle database. This article will introduce in detail how to query the version information of the Oracle database and give specific code examples. Query the database version of the SQL statement in the Oracle database by executing a simple SQL statement

See all articles