Zend variable exploration

zhaozj2021-02-16  44

Zend variable exploration

V2.0

TAFT@wjl.cn

latest update

2006-1-27

[Abstract] This article describes the mechanism of the Zend Engine to process script variables as the core of the PHP language.

Write C, C or Java code is definitely clear, you must define a variable before using the variable, such as integer int, character type char, etc., which is automatically saved to variables when compiling. space. First define (some must also initialize), use the basic principle of compile language, otherwise it cannot pass. The interpreted language does not have such a "trouble". What variables want to use according to naming rules are used, do not declare. This is really convenient, especially suitable for beginner computer programming, such as the Basic language is an authentic interpreted language. Can you notice, trouble? Yes, the birth of the variable in the interpretation language is troublesome, and the work is not less than the compilation language. The PHP of the open original code is also the interpretation language.

1. Definition of variables in Zend:

Zend's variable is divided into internal variables and external variables, and internal variables are mainly the "internal employee", definitions and C locales in engine actions or expansion modules. The external variable is the variable used in the PHP script file scanned to the Zend Engine and a part of the parameter of the PHP function is defined as ZVAL. Please see the definition below:

Typedef struct _zval_struct zval;

TYPEDEF STRUCT _ZEND_CLASS_ENTRY ZEND_CLASS_ENTRY

Typedef union _zvalue_value {

Long Lval; / * long value * /

Double Dval; / * double value * /

Struct {

Char * Val;

Int Len;

} STR;

Hashtable * ht; / * hash table value * /

Struct {

Zend_Class_ENTRY * CE;

Hashtable * Properties;

} OBJ;

ZVALUE_VALUE;

Struct _zval_struct {

/ * Variable Information * /

ZVALUE_VALUE VALUE; / * VALUE * /

Zend_UCHAR TYPE; / * ACTIVE TYPE See 3 * /

Zend_UCHAR IS_REF; / * is a reference * /

Zend_ushort refcount;

}

(Zend.h)

Zend uses a container to handle all external variables, this container is the union (Union) _zValue_value above, which contains common long-intensive (long), floating-point double-precision type (Double), string type (structure) Body, there is a string address, string length, hash table, class structure (if you define a class, this structure works). Typically, in C containers are defined as a class that saves a batch of objects as the main purpose. The role of the Zend variable container is clearly similar to C , and the container presents different types according to the different assignments of the user. Zend puts the external variable in the structural body_zal_struct, the structure stores the basic information of the variable: Value, Variety Type (Type), is a reference (PHP4 or more), reference count (more PHP4). Define an external variable as follows: ZVAL my_zval, * PMY_ZVAL;

Quote the value of a variable: my_zval.Value; get the variable type: my_zval.type

It is also necessary to mention that Type here is defined by Zend, not atomic types in the C language. Listed as follows:

/ * data types * /

#define is_null 0

#define is_long 1

#define is_double 2

#define is_string 3

#define is_ARRAY 4

#define is_Object 5

#define is_bool 6

#define is_resource 7

#define is_constant 8

#define is_constant_Array 9

(Zend.h 318 line)

These types are not used to define variables, mainly used as a judgment of the type.

The following figure is clearer reflects mutual relationships:

Struct _zval_struct {

/ * Variable information zvalue_value value; zend_uchar type;

Zend_uchar is_ref; zend_ushort refcount;

}

Typedef union _zvalue_value {

Long Lval; / * long value * /

Double Dval; / * double value * /

Struct {

Char * Val;

Int Len;

} STR;

Hashtable * ht; / * hash table value * /

Struct {

Zend_Class_ENTRY * CE;

Hashtable * Properties;

} OBJ;

ZVALUE_VALUE;

IS_NULL 0

IS_LONG 1

IS_DOUBLE 2

IS_STRING 3

IS_ARRAY 4

IS_Object 5

IS_BOOL 6IS_RESOURCE 7

IS_CONSTANT 8

IS_CONSTANT_ARRAY 9

2, the variable is created in Zend

Zend Scan Interprets the external variable and uses ZVAL definitions, you must apply for a suitable space for this variable in memory, and then add the variable to the symbol table. If it is a global variable, it is added to the global symbol table. First, Zend used a macro: make_std_zval

#define make_std_zval (zv) / / / Assign new ZVAL container

Alloc_zval (zv); /

INIT_PZVAL (ZV);

(Zend.h)

This macro calls alloc_zval allocates the appropriate space in memory, and then uses the init_pzval (zv) initialization variable: set the container reference count (refunt) is 1, whether it is referenced by IS_REF (0),

The next step of Zend is to add the variable to the symbol table, use macro Zend_SET_SYMBOL (SymTable, Name, Var), which checks if this value already exists in the symbol table, if there is, turn new symbol into a reference (Reference) Variables (and automatically release the old ZVAL container).

3, for example

Finally, a whole process of several variables is given here:

(1) Assume that the user has a variable in the PHP script file $ new_long_name = 100

The processing of the Zend engine is as follows:

ZVAL * new_long; / / Define a container variable

Makt_std_zval (new_long); / / open memory space for new variables and initialize

NEW_LONG-> TYPE = IS_LONG;

NEW_LONG-> Value.lval = 100;

Zend_SET_SYMBOL (EG (Active_Symbol_Table), "New_LONG_NAME", New_LONG); // Add Variable to Symbol Table

Zend_SET_SYMBOL uses the EG macro to regulate the Global variable. By specifying the EG (Active_Symbol_Table), you can access the current active symbol table, process the activity, and the partial range. Different local scope according to function calls will also change.

(2) Creation of strings:

ZVAL * new_string;

Char * String_Contents = "this is a new string variable";

Make_std_zval (new_string);

NEW_STRING-> TYPE = IS_STRING;

NEW_STRING-> Value.Str.len = Strlen (String_Contents);

NEW_STRING-> Value.Str.val = estrdup (string_contents);

转载请注明原文地址:https://www.9cbs.com/read-26842.html

New Post(0)