Source code research: php variables

One: There are several types of variables in php

1. Scalar type: Boolean type bool, integer type int, floating point type float, string type string
2. Complex type: array array, object object
3. Special type: NULL,
how are these variables realized? We all know that php is implemented in c language, so how is it implemented in c language?
Let's take a look at the source code of php5.5.7 and see how it is implemented. The most important thing is the zval structure.

Two: the definition of zval

在 zend/zend_types.h
typedef struct _zval_struct zval

_zval_struct This structure is defined in zend/zend.h

struct _zval_struct {
    /* Variable information */
    zvalue_value value; /* value */
    zend_uint refcount__gc;
    zend_uchar type; /* active type */
    zend_uchar is_ref__gc;
};

 

There is a zvalue_value value in the _zval_struct structure, which is the value stored by the variable, and
zend_uchar type is the type of the variable. To judge the type of a variable, it is judged by this type.


What is the type of zvalue_value
? It is a union, defined as follows:

typedef union _zvalue_value {
    long lval; /* long value */
    double dval; /* double value */
    struct {
      char *val;
      int len;
    } str;
    HashTable *ht; /* hash table value */
    zend_object_value obj;
} zvalue_value;

 

Do you see, use a union to define all data types in php

zend_uchar type
variable type definition, in zend.h, the following types are defined:

#define IS_NULL 0
#define IS_LONG 1
#define IS_DOUBLE 2
#define IS_BOOL 3
#define IS_ARRAY 4
#define IS_OBJECT 5
#define IS_STRING 6
#define IS_RESOURCE 7
#define IS_CONSTANT 8
#define IS_CONSTANT_ARRAY 9
#define IS_CALLABLE 10

 

zend_uint refcount__gc
This is related to the garbage collection of variables, php5.2, etc. and used to use reference counting for garbage collection. After php5.3, a new garbage collection algorithm Concurrent Cycle Collection in Reference Counted Systems was introduced, which solved the problem of circular references.

Other zend_uchar, zend_uint
, etc. are defined in the type zend/zend_types.h that encapsulates the c language

typedef unsigned char zend_bool;
typedef unsigned char zend_uchar;
typedef unsigned int zend_uint;
typedef unsigned long zend_ulong;
typedef unsigned short zend_ushort;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324624129&siteId=291194637