Home  >  Article  >  Backend Development  >  PHP Kernel Exploration: Variable Storage and Type Usage Instructions_PHP Tutorial

PHP Kernel Exploration: Variable Storage and Type Usage Instructions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:45780browse

Let’s answer the question in the previous section first.

Copy code The code is as follows:

$foo = 10;
$bar = 20;

function change() {
global $foo;
//echo 'Internal function $foo = '.$foo.'
';
/ /If $bar is not defined as a global variable, $bar cannot be accessed within the function body.
$bar = 0;
$foo++;
}

change (); echo $foo, ' ', $bar;
?>

Program output 11 20. The reason is that the $bar variable cannot be accessed inside the method, so its value is still 20. After using global, you can get the value of $foo. After the auto-increment, the value of $foo is 11.

Global is used to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require.
The three basic characteristics of variables are mentioned in the preface. One of them is the type of variables. Variables have specific types, such as strings, arrays, objects, etc. The type system of programming languages ​​can be divided into two types: strong type and weak type:
Strongly typed language means that once a variable is declared as a variable of a certain type, the type of the variable cannot be changed during the running of the program. Values ​​other than other values ​​are assigned to it (of course, this is not entirely the case. This may involve type conversion, which will be introduced in the following sections). Languages ​​such as C/C++/Java fall into this category.
Script languages ​​such as PHP, Ruby, and JavaScript are weakly typed languages: a variable can represent any data type.
A large part of the reason why PHP has become a simple and powerful language is that it has weakly typed variables. But sometimes this is also a double-edged sword, and improper use can cause some problems. Just like an instrument, the more powerful it is, the greater the potential for error.
In the official PHP implementation, all variables are stored using the same data structure (zval), and this structure also represents various data types in PHP. It contains not only the value of the variable, but also the type of the variable. This is the core of PHP's weak typing.
So how does the zval structure implement weak types? Let’s uncover the veil together.
Variable storage structure
PHP does not need to explicitly indicate its data type when declaring or using variables.
PHP is a weakly typed language, which does not mean that PHP has no types. In PHP, there are 8 variable types, which can be divided into three categories
* Scalar types: boolean, integer, float (double), string
* Composite types: array, object
* Special types: resource, NULL
Official PHP is implemented in C, and C is a strongly typed language, so how does this implement weak typing in PHP? ?
The value of the variable is stored in the zval structure shown below. The zval structure is defined in the Zend/zend.h file, and its structure is as follows:

Copy code The code is as follows:
typedef struct _zval_struct zval;
...
struct _zval_struct {
/* Variable information */
zvalue_value value; /* value */
zend_uint refcount__gc;
zend_uchar type; /* active type */
zend_uchar is_ref__gc;
};

PHP uses this structure to store all data for variables. Unlike other compiled static languages, PHP also saves the PHP user space variable type in the same structure when storing variables. In this way we can obtain the type of the variable through this information.
There are four fields in the zval structure, their meanings are:

属性名 含义 默认值
refcount__gc 表示引用计数 1
is_ref__gc 表示是否为引用 0
value 存储变量的值
type 变量具体的类型

After PHP5.3, a new garbage collection mechanism was introduced, and the reference counting and referenced field names were changed to refcount__gc and is_ref__gc. Before that were refcount and is__ref.

The value of the variable is stored in another structure zvalue_value. Value storage is described below.
PHP user space refers to the level of PHP language, and most of this book discusses the implementation of PHP. These implementations can be understood as kernel space. Since PHP is implemented in C, the scope of this space will be limited to C language. The PHP user space will be limited to the scope of PHP syntax and functions provided. For example, some PHP extensions provide some PHP functions or classes, which export methods or classes to the PHP user space.
Variable type
The type field of the zval structure is the most critical field to implement weak typing. The value of type can be one of: IS_NULL, IS_BOOL, IS_LONG, IS_DOUBLE, IS_STRING, IS_ARRAY, IS_OBJECT and IS_RESOURCE. It's easy to understand literally. They are just unique identifiers of types, and different values ​​are stored in the value field according to different types. In addition, the types defined with them are IS_CONSTANT and IS_CONSTANT_ARRAY.
This is similar to what we do when designing a database. In order to avoid duplicating the design of similar tables, we use an indicator field to record different types of data.

The value of the variable is stored
As mentioned earlier, the value of the variable is stored in the zvalue_value union. The structure is defined as follows:

Copy code The code is 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;

这里使用联合体而不是用结构体是出于空间利用率的考虑,因为一个变量同时只能属于一种类型。 如果使用结构体的话将会不必要的浪费空间,而PHP中的所有逻辑都围绕变量来进行的,这样的话, 内存浪费将是十分大的。这种做法成本小但收益非常大。
各种类型的数据会使用不同的方法来进行变量值的存储,其对应赋值方式如下:

1. 一般类型

Variable type Macro ?
boolean ZVAL_BOOL Boolean/integer variable values ​​are stored in (zval).value.lval, and their types will also be stored with the corresponding IS_*.
Z_TYPE_P(z)=IS_BOOL/LONG; Z_LVAL_P(z)=((b)!=0);
integer ZVAL_LONG
float ZVAL_DOUBLE
null ZVAL_NULL NULL值的变量值不需要存储,只需要把(zval).type标为IS_NULL。
Z_TYPE_P(z)=IS_NULL;
resource ZVAL_RESOURCE 资源类型的存储与其他一般变量无异,但其初始化及存取实现则不同。
Z_TYPE_P(z) = IS_RESOURCE; Z_LVAL_P(z) = l;

2. String Sting
The type indication of string is the same as other data types, but there is an additional field for string length when storing strings.

Copy code The code is as follows:

struct {
char *val;
int len;
} str;

The character string in C starts with

3. Array

Array is the most commonly used and most powerful variable type in PHP. It can store other types of data and provides various built-in operation functions. The storage of arrays is more complicated than other variables. The value of the array is stored in the zvalue_value.ht field, which is a HashTable type data. PHP's arrays use hash tables to store associated data. A hash table is an efficient key-value pair storage structure. Two data structures, HashTable and Bucket, are used in PHP's hash table implementation. All the work of PHP is implemented by hash tables. In the next section HashTable, we will introduce the basic concepts of hash tables and the implementation of hash tables in PHP.

4. Object

In object-oriented languages, we can define the data types we need, including class attributes, methods and other data. An object is a specific implementation of a class. Objects have their own state and operations they can complete.

PHP’s object is a composite data, which is stored using a zend_object_value structure. Its definition is as follows:

Copy code The code is as follows:
typedef struct _zend_object_value {
zend_object_handle handle; // unsigned int type, EG( objects_store).object_buckets index
zend_object_handlers *handlers;
} zend_object_value;

PHP objects are only created at runtime. The previous chapter introduced the EG macro, which is a global structure used to save data at runtime. This includes the object pool used to save all created objects, EG (objects_store), and the zend_object_handle field of the object object value content is the index of the current object in the object pool, and the handlers field is the index when the object is operated. The processing function is saved. The structure _zend_class_entry of this structure and object-related classes will be introduced later.

PHP’s weak variable container is implemented in an inclusive form, and each type of variable has its corresponding tag and storage space. Languages ​​that use strong types are usually more efficient than weak types because a lot of information can be determined before running, which can also help eliminate program errors. The problem this brings is that writing code is relatively restricted.

PHP is mainly used as a web development language. In ordinary web applications, the bottleneck is usually at the business and data access layer. However, language will also be a key factor in large applications. Facebook therefore uses its own PHP implementation. Compile PHP to C++ code to improve performance. However, Facebook's hiphop is not a complete PHP implementation. Since it directly compiles PHP into C++, some dynamic features of PHP such as the eval structure cannot be implemented. Of course, there are ways to implement it. If hiphop does not implement it, it should be a trade-off.

http://www.bkjia.com/PHPjc/728099.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/728099.htmlTechArticleLet’s answer the question in the previous section first. Copy the code as follows: ?php $foo = 10; $bar = 20; function change() { global $foo; //echo 'Internal function $foo = '.$foo.'br /'; //If not. ..
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn