Home Backend Development PHP Tutorial PHP变量在zend内核中的储存方式

PHP变量在zend内核中的储存方式

Jun 13, 2016 pm 12:23 PM
define zend zval

PHP变量在zend内核中的存储方式

PHP中的变量可以保存任何的数据类型,这是因为它是弱类型语言。但php是用C语言编写的,C语言是强类型语言,每个变量都有固定的类型,不能随意改变变量的类型(可以通过强制类型转变,不过有可能出现问题),在zend引擎中是怎样可以做到一个变量保存任何类型的呢?
在zend/zend.h头文件中,会发现下面的结构体:

1

<code>typedef struct _zval_struct zval;typedef union _zvalue_value {    long lval;    double dval;    struct {        char *val;        int len;    }str;    HashTable *ht;    zend_object_value obj;} zvalue_value ;struct _zval_struct {    zvalue_value value;    zend_uint refcount;    zend_uchar type;    zend_uchar is_ref;};</code>

Copy after login

zval结构体就是通常用到的PHP变量在内核中的表达方式。在zval结构体中,可以看到4个成员变量,分别是:

1

<code>zvalue_value value;  //变量的值,PHP变量的值就保存在这里zend_uint refcount;  //变量引用数,变量引用计算器zend_uchar type;     //变量的类型zend_uchar is_ref;   //变量是否被引用</code>

Copy after login

zval结构体的value成员变量是一个zvalue_value联合体,PHP能够保持任何的结构类型就是因为这个联合体 。从zvalue_value联合体的成员变量中可以看到,不同类型会保存到不同的成员变量中,这样就实现了PHP变量可以存储任何数据类型。例如,当变量是整数类型时,会保存到value的lval成员变量中;当变量的类型是字符串时,又会保存到value的str成员变量中。

不同类型对应的成员变量
还有一个问题,就是zend引擎是怎么知道这个变量保存的是什么类型呢?我们注意到,zval结构体中有个type成员变量,这个成员变量就是要保存一个php变量的类型。
zend引擎定义了8中变量类型:

1

<code>#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</code>

Copy after login

每一个宏定义对应php语言层的一种类型,例如当zval的type成员变量等于IS_STRING时(zval.type==IS_STRING),说明这个变量的类型是字符串类型。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

defineHow to define multi-line macros defineHow to define multi-line macros Oct 11, 2023 pm 01:24 PM

define defines a multi-line macro by using `\` to divide `do { \ printf("%d\n", x); \ } while (0)` into multiple lines for definition. In a macro definition, the backslash `\` must be the last character of the macro definition and cannot be followed by spaces or comments. When using `\` for line continuation, be careful to keep the code readable and make sure there is a `\` at the end of each line.

Explore the importance and role of define function in PHP Explore the importance and role of define function in PHP Mar 19, 2024 pm 12:12 PM

The importance and role of the define function in PHP 1. Basic introduction to the define function In PHP, the define function is a key function used to define constants. Constants will not change their values ​​during the running of the program. Constants defined using the define function can be accessed throughout the script and are global. 2. The syntax of define function The basic syntax of define function is as follows: define(&quot;constant name&quot;,&quot;constant value&amp;qu

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

How does PHP store variables? Do you understand the zval structure? How does PHP store variables? Do you understand the zval structure? May 26, 2022 am 09:47 AM

There is no need to declare a type when defining a variable in PHP. You initially assign an integer value to the variable $a, and you can easily change it to other types later. So how is this variable $a stored in the PHP source code? With this question in mind, let’s take a look at the source code of PHP.

defineHow to define conditional compilation defineHow to define conditional compilation Oct 11, 2023 pm 01:20 PM

defineConditional compilation can be achieved using the `#ifdef`, `#ifndef`, `#if`, `#elif`, `#else` and `#endif` preprocessing directives.

define usage of function macro define usage of function macro Oct 11, 2023 pm 12:00 PM

The usage of define function macro: 1. Define a simple calculation macro, "#define SQUARE(x) ((x) * (x))"; 2. Define a macro with multiple parameters, "#define MAX(a , b) ((a) > (b) ? (a) : (b))"; 3. Define macros with complex expressions, "#define ABS(x) ((x) < 0 ? -(x ) : (x))”.

The difference between typedef and define The difference between typedef and define Sep 26, 2023 am 10:41 AM

The difference between typedef and define lies in type checking, scope, readability, error handling, memory usage, etc. Detailed introduction: 1. Type checking, the type alias defined by typedef is a real type, and type checking will be performed, while the macro defined by define is just a simple text replacement, and type checking will not be performed; 2. Scope, the type alias defined by typedef The scope of is local and only valid within the current scope, while the macro defined by define is global and can be used anywhere; 3. Readability, etc.

See all articles