Home php教程 php手册 php 全局变量:PHP源码分析-弱类型变量实现

php 全局变量:PHP源码分析-弱类型变量实现

Jun 21, 2016 am 08:50 AM
define typedef zend zval


PHP是弱类型,动态的语言脚本。在申明一个变量的时候,并不需要指明它保存的数据类型。
例如:
$var = 1;
$var = "variable";
$var = 1.00;
$var = array();
$var = new Object();
动态变量,在运行期间是可以改变的,并且在使用前无需声明变量类型。
那么,问题一、Zend引擎是如何用C实现这种弱类型的呢?
实际上,在PHP中声明的变量,在ZE中都是用结构体zval来保存的。
首先我们打开Zend/zend.h来看zval的定义:
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;
};
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;
Zend/zend_types.h:
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;
从上述代码中,可以看到_zvalue_value是真正保存数据的关键部分。通过共用体实现的弱类型变量声明
问题二、Zend引擎是如何判别、存储PHP中的多种数据类型的呢?
_zval_struct.type中存储着一个变量的真正类型,根据type来选择如何获取zvalue_value的值。
type值列表(Zend/zend.h):
#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
来看一个简单的例子:
$a = 1;
//此时zval.type = IS_LONG,那么zval.value就去取lval.
$a = array();
//此时zval.type = IS_ARRAY,那么zval.value就去取ht.
这其中最复杂的,并且在开发第三方扩展中经常需要用到的是"资源类型".
在PHP中,任何不属于PHP的内建的变量类型的变量,都会被看作资源来进行保存。 本文链接http://www.cxybl.com/html/wlbc/Php/20121213/34971.html



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Usage of typedef struct in c language Usage of typedef struct in c language May 09, 2024 am 10:15 AM

typedef struct is used in C language to create structure type aliases to simplify the use of structures. It aliases a new data type to an existing structure by specifying the structure alias. Benefits include enhanced readability, code reuse, and type checking. Note: The structure must be defined before using an alias. The alias must be unique in the program and only valid within the scope in which it is declared.

What is the usage of typedef What is the usage of typedef Sep 04, 2023 pm 01:20 PM

The use of typedef is to create new aliases for existing data types. Using typedefs can increase the readability and maintainability of your code, especially when dealing with complex data types. For simple data types, such as integers, floating point numbers, or characters, the benefits of using aliases are not obvious. However, for complex data types such as pointers, structures, arrays, and functions, the advantages of using aliases are obvious. A typedef cannot be used before a variable or function definition and is usually created at the top of a program file or after a structure definition.

Understanding Memory Management of C++ Function Pointers: Avoiding Pointer Traps Understanding Memory Management of C++ Function Pointers: Avoiding Pointer Traps Apr 29, 2024 pm 09:03 PM

When using function pointers in C++, memory management must be carefully considered to avoid pitfalls. These traps include dangling pointers (pointing to functions outside their scope) and wild pointers (function pointers that are never initialized or set to nullptr). To avoid these pitfalls, follow these best practices: always initialize function pointers, manage memory carefully, and use smart pointers. This way you can use function pointers safely and avoid falling into pointer traps.

Detailed explanation of Linux kernel timer and delay work driver development Detailed explanation of Linux kernel timer and delay work driver development Feb 13, 2024 am 11:57 AM

Linux kernel timers and delay tasks are two commonly used mechanisms to implement scheduled tasks and delayed execution tasks. They allow the driver to execute specific functions at the appropriate time point to adapt to the needs and characteristics of the hardware device. But how do you properly use Linux kernel timers to work with delays? This article will introduce the basic knowledge and skills of Linux kernel timer and delay work driver development from both theoretical and practical aspects, as well as some common problems and solutions. The timer on the kernel timer software ultimately relies on the hardware clock. Simply put, the kernel will detect whether each timer registered to the kernel has expired after the clock interrupt occurs. If it expires, it will call back the corresponding registration function. Do this as an interrupt to the bottom half. Reality

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("constant name","constant value&qu

How to write simple fireworks code in C language How to write simple fireworks code in C language Apr 13, 2024 pm 09:18 PM

To write a simple firework code in C, you need to follow these steps: Include header files and libraries. Define constants and macros. Create a particle data structure. Declare global variables. Initialize the fireworks particles in the main() function. Update the particle's position and velocity in the game loop and draw them. Check for and destroy particles that have reached their end of life.

The difference between typedef struct and struct in c++ The difference between typedef struct and struct in c++ May 01, 2024 am 11:36 AM

The difference between typedef struct and struct: typedef struct creates an alias of a structure type, while struct defines a new structure type. The alias created by typedef struct can be used after it is declared, while the structure defined by struct can be used after it is defined. Neither typedef struct nor struct creates additional storage space.

See all articles