Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the implementation of variables in PHP7

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the implementation of variables in PHP7". In daily operation, I believe many people have doubts about how to understand the implementation of variables in PHP7. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to understand the implementation of variables in PHP7". Next, please follow the editor to study!

Complex zval data values have a common header, whose structure is defined by zend_refcounted:

Struct _ zend_refcounted {uint32_t refcount; union {struct {ZEND_ENDIAN_LOHI_3 (zend_uchar type, zend_uchar flags, uint16_t gc_info)} v; uint32_t type_info;} u;}

This header stores refcount (reference count), the type type of the value and the information about recycling gc_info, as well as the type flag flags.

Next, each complex type of implementation is analyzed separately and compared with the implementation of PHP5. Although references are also complex types, they have been introduced in the previous section, so I won't repeat them here. In addition, resource types are not mentioned here (because the author thinks there is nothing to say about resource types).

String

A new structure, zend_string, is defined in PHP7 to store string variables:

Struct _ zend_string {zend_refcounted gc; zend_ulong h; / * hash value * / size_t len; char val [1];}

In addition to referencing the header of the count, the string contains the hash cache h, the string length len, and the value val of the string. The hash cache exists to prevent a key that uses a string as a hashtable from repeatedly calculating its hash value when looking up, so this initializes it before it is used.

If you don't know much about the C language, you may find the definition of val a little strange: this declaration has only one element, but obviously the string payment we want to store must be greater than one character in length. What is actually used here is a "black" method of the structure: only one element is defined when the array is declared, but enough memory is allocated to store the entire string when the zend_string is actually created. In this way, we can still access the complete string through val.

Of course, this is an unconventional means of implementation, because what we actually read and write exceeds the boundaries of a single-character array. But the C language compiler doesn't know what you're doing. Although C99 has explicitly specified support for "flexible arrays", thanks to our good friend Microsoft, no one can guarantee the consistency of C99 on different platforms (so this approach is to solve the problem of flexible array support on the Windows platform).

The structure of the new string type is easier to use than the native C string: the first is that the length of the string is directly stored so that you don't have to calculate it every time you use it. The second is that the string also has a reference count header, so that the string itself can be shared in different places without using zval. A common place to use is the key that shares hashtable.

But the new string type also has a downside: although it's easy to pull the C string out of zend_string (using str- > val), conversely, if you change the C string to zend_string, you need to allocate the memory needed by zend_string before copying the string into zend_string. This is not very convenient in the process of practical use.

Strings also have some unique flags (stored in the flag bits of GC):

# define IS_STR_PERSISTENT (1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report