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

Stored procedure Source Code Analysis of PHP variables

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "PHP variable stored procedure source code analysis". In daily operation, I believe that many people have doubts about the stored procedure source code analysis of PHP variables. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "stored procedure source code analysis of PHP variables". Next, please follow the editor to study!

The PHP code is as follows:

The copy code is as follows:

$php_var = 1

The code corresponding to C is:

The copy code is as follows:

Zval* censor var. / / define PHP variable pointers

MAKE_STD_ZVAL (c_var); / / initialize the PHP variable

ZVAL_LONG (centering var.1); / / assignment

ZEND_SET_SYMBL (EG (active_symbol_table), "php_var", c_var); / / registered to the global variable symbol table

First of all, look at the first line: zval* centering varabash / declare a zval pointer cymbal var. the structure of the zval is as follows:

The copy code is as follows:

Struct _ zval_struct {

/ * Variable information * /

Value of zvalue_value value; / * variable * /

Zend_uint refcount; / * reference count, used for garbage collection * /

Zend_uchar type; / * variable type * /

Whether zend_uchar is_ref; / * is a reference variable * /

}

Typedef struct _ zval_struct zval

The structure of the value zvalue_value is as follows:

The copy code is as follows:

Typedef union _ zvalue_value {

Long lval; / * long plastic surgery * /

Double dval; / * double precision type * /

Struct {/ * value of string type * /

Char * val

Int len

} str

HashTable * ht; / * value of array type * /

Zend_object_value obj; / * value of object type * /

} zvalue_value

2. Next, take a look at the second line: MAKE_STD_ZVAL (new_val); / / Macro related to variable initialization is as follows: / / initialization

The copy code is as follows:

# define MAKE_STD_ZVAL (zv)\

ALLOC_ZVAL (zv);\

INIT_PZVAL (zv)

# define ALLOC_ZVAL (z)\

ZEND_FAST_ALLOC (z, zval, ZVAL_CACHE_LIST)

# define ZEND_FAST_ALLOC (p, type, fc_type)\

(P) = type *) emalloc (sizeof (type))

# define INIT_PZVAL (z)\

(Z)-> refcount = 1;\

(Z)-> is_ref = 0

After deployment, it is:

The copy code is as follows:

(c_var) = (zval *) emalloc (sizeof (zval)); / / allocate memory

(c_var)-> refcount = 1; / / reference count initialization

(c_var)-> is_ref = 0; / / whether to refer to

As you can see, its function is to allocate memory and initialize refcount,is_ref.

3. Let's take a look at the third line: ZVAL_LONG (cymbal varreco1) related macros are:

The copy code is as follows:

/ / define the value

# define ZVAL_LONG (z, l) {\

Z_TYPE_P (z) = IS_LONG;\

Z_LVAL_P (z) = 1;\

}

# define Z_TYPE_P (zval_p) Z_TYPE (* zval_p)

# define Z_TYPE (zval) (zval). Type

# define Z_LVAL_P (zval_p) Z_LVAL (* zval_p)

# define Z_LVAL (zval) (zval). Value.lval

After deployment, it is:

The copy code is as follows:

(* c_var) .type = IS_LONG

(* c_var) .value = 1

Four: next, look at the fourth line: ZEND_SET_SYMBOL (EG (active_symbol_table), "php_var", c_var); first of all, it is stated that the variable of PHP exists in a hashtable.

The copy code is as follows:

Struct _ zend_executor_globals {

... .

Symbol table for HashTable symbol_table;// global variables

Symbol table of HashTable * active_symbol_table;// local variables

... ..

}

The Key of Hashtable is the name of the variable, namely php_var, and the value is the pointer to the PHP variable, that is, the c_var pointer. The related macros are:

The copy code is as follows:

# define ZEND_SET_SYMBOL (symtable, name, var)\ {\

Char * _ name = (name);\

ZEND_SET_SYMBOL_WITH_LENGTH (symtable, _ name, strlen (_ name) + 1, var, 1,0);\

}

/ / the main implementation is the following function:

# define ZEND_SET_SYMBOL_WITH_LENGTH (symtable, name, name_length, var, _ refcount, _ is_ref)\

{

Zval * * orig_var;\

If (zend_hash_find (symtable, (name), (name_length), (void * *) & orig_var) = = SUCCESS\

& PZVAL_IS_REF (* orig_var)) {\

(var)-> refcount = (* orig_var)-> refcount;\

(var)-> is_ref = 1;\

If (_ refcount) {\

(var)-> refcount + = _ refcount-1;\

}\

Zval_dtor (* orig_var);\

* * orig_var = * (var);\

FREE_ZVAL (var);\

} else {\

(var)-> is_ref = _ is_ref;\

If (_ refcount) {\

(var)-> refcount = _ refcount;\

}\

Zend_hash_update (symtable, (name), (name_length), & (var), sizeof (zval *), NULL);\

}\

}

The function of this function is:

1. If the variable already exists in the global symbol table and is a reference type,

a. Assign the reference count refcount,is_ref information of the original variable to c_var

b. Release the value of the original variable zvalue, for example, if its value points to a mysql connection resource, release the resource.

c. Assign the variable pointed to by c_var to the original variable d. Freeing up the memory space of the c_var ensures that if the variable is applied, the value changes together. For example, if there is $baked rooma in front of it

two。 If the variable does not exist in the global symbol table or if it exists but is not a reference variable, change its value directly.

At this point, the study of "stored procedure source code analysis of PHP variables" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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