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

Example Analysis of PHP7 basic variables

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

Share

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

This article mainly introduces the example analysis of PHP7 basic variables, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The basic structure of PHP7's basic variables

We all know that variables in PHP are weakly typed and do not need to be specified when declared. So how exactly does this come true? This starts with the infrastructure of variables.

The realization of zval

In the source file zend_type.h, you can see the definition of zval:

Typedef struct _ zval_struct zval;struct _ zval_struct {zend_value value / * value * / union {struct {ZEND_ENDIAN_LOHI_4 (zend_uchar type, / * active type * / zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) / * call info for EX (This) * /} v Uint32_t type_info;} U1; union {uint32_t next; / * hash collision chain * / uint32_t cache_slot; / * literal cache slot * / uint32_t lineno; / * line number (for ast nodes) * / uint32_t num_args / * arguments number for EX (This) * / uint32_t fe_pos; / * foreach position * / uint32_t fe_iter_idx; / * foreach iterator index * / uint32_t access_flags; / * class constant access flags * / uint32_t property_guard / * single property guard * / uint32_t extra; / * not further specified * /} U2;}

The structure of zval consists of a union complex zend_value that holds values or pointers of variable types, and two union combinations U1 and U2.

U1

The function of U1 is to store variable types and their information, and the fields in U1 are used as follows:

Type: record the variable type. Can be accessed through u2.v.type

Type_flags: tags of specific types of corresponding variables (such as constant types, reference counting types, immutable types). Different types of variables have different flag.

Const_flags: tag of constant type

Reserved: reserved field

U2

U2 is mainly an auxiliary function, because the memory of the structure is aligned, so this space of U2 with or without U2 has already occupied the space, so it is used. Many types of information are recorded in the auxiliary fields of U2, which are of great benefit to internal functions, or improve cache friendliness or reduce memory addressing operations. Some of these fields are introduced here.

Next: used to resolve hash conflicts (the hash conflict is not yet understood) and record the location of the next element of the conflict.

Cache_slot: run-time cache. When executing the function, it will first look up it in the cache, and if it is not in the cache, then look it up in the global function table.

Num_args: the number of parameters passed in when the function is called

Access_flags: the access identity of the object class, such as public protected private.

Zend_value

Typedef union _ zend_value {zend_long lval; / * integer * / double dval; / * floating point * / zend_refcounted * counted; zend_string * str; zend_array * arr; zend_object * obj; zend_resource * res; zend_reference * ref Zend_ast_ref * ast; zval * zv; void * ptr; zend_class_entry * ce; zend_function * func; struct {uint32_t w1; uint32_t w2;} ww;} zend_value

As you can see from zend__value, the long and double types store values directly, while the other types are pointers to their respective structures. So, because of the structure like zval, the PHP variable does not have to specify its type when it is declared, because no matter what type of value you assign to the variable, it will help you find the corresponding storage structure.

Take a variable whose value is a string as an example, its structure is as follows:

Comparison of zval structure between PHP5 and PHP7

PHP5

PHP7

You can see that the total zval of php7 is only 16 bytes, which saves a lot of memory compared to the 48 bytes occupied by PHP5's zval.

In addition, in PHP5, all variables are applied in the heap, but for temporary variables, it is not necessary to apply in the heap. So this is optimized in PHP7, and temporary variables are applied directly on the stack.

Common variable types

The following describes several common types of variable structures, other more types, you can check the source code.

Integer and floating point type

For integers and floating-point types, because of their small footprint, the values of integers stored directly in zval are stored in lval, while floating-point values are stored in dval.

Typedef union _ zend_value {zend_long lval; / * integer * / double dval; / * floating point * /...} string

A new string structure is defined in PHP 7. The structure is as follows:

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

The meaning of each of the above fields:

Gc: variable reference information, which is the structure of all variable types that use reference counting.

H: hash value, which is used when calculating the index in the array. (this operation is said to improve the performance of PHP7 by 5%)

Len: string length, which is used to ensure binary security

Val: string content, variable length struct. Memory is allocated according to len length.

Array

Array is a very powerful data structure in PHP, and its underlying implementation is ordinary ordered HashTable. Here's a brief look at its structure. The follow-up will be more specific and in-depth.

Typedef struct _ zend_array HashTable;struct _ zend_array {zend_refcounted_h gc; union {struct {ZEND_ENDIAN_LOHI_4 (zend_uchar flags, zend_uchar nApplyCount, zend_uchar nIteratorsCount, zend_uchar consistency)} v; uint32_t flags;} u Uint32_t nTableMask; Bucket * arData; uint32_t nNumUsed; uint32_t nNumOfElements; uint32_t nTableSize; uint32_t nInternalPointer; zend_long nNextFreeElement; dtor_func_t pDestructor;} object

The object structure of PHP7 has also been redesigned, which is very different from the implementation of PHP5.

Struct _ zend_object {zend_refcounted_h gc; uint32_t handle; zend_class_entry * ce; const zend_object_handlers * handlers; HashTable * properties; zval properties_table [1];}

Here are some of the fields:

Gc:gc header

* ce: the class class corresponding to the object

* properties: HashTable structure. Key is the attribute name of the object, and value is the offset of the attribute value in the properties_tables array. Find the corresponding attribute value in properties_talbe through the offset.

Properties_talbe [1]: stores the property values of an object

Ok, write this and stop here first.

Thank you for reading this article carefully. I hope the article "sample Analysis of PHP7 basic variables" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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