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


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
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
typedef的用法是什么typedef的用法是什么Sep 04, 2023 pm 01:20 PM

typedef的用法是为已经存在的数据类型创建新的别名。使用typedef可以增加代码的可读性和可维护性,特别是在处理复杂的数据类型时。对于简单的数据类型,如整数、浮点数或字符,使用别名的好处并不明显。然而,对于指针、结构体、数组和函数等复杂的数据类型,使用别名的优势就显而易见了。typedef不能用于变量或函数定义之前,通常在程序文件的顶部或结构体定义之后创建。

Linux内核定时器与延迟工作驱动开发详解Linux内核定时器与延迟工作驱动开发详解Feb 13, 2024 am 11:57 AM

Linux内核定时器与延迟工作是两种常用的实现定时任务和延后执行任务的机制,它们可以让驱动程序在合适的时间点执行特定的函数,以适应硬件设备的需求和特性。但是,如何正确地使用Linux内核定时器与延迟工作呢?本文将从理论和实践两方面,介绍Linux内核定时器与延迟工作驱动开发的基本知识和技巧,以及一些常见的问题和解决方法。内核定时器软件上的定时器最终要依靠硬件时钟来实现,简单的说,内核会在时钟中断发生后检测各个注册到内核的定时器是否到期,如果到期,就回调相应的注册函数,将其作为中断底半部来执行。实

define怎么定义多行宏define怎么定义多行宏Oct 11, 2023 pm 01:24 PM

define定义多行宏可以通过使用 `\` 将 `do { \ printf("%d\n", x); \ } while (0)` 分成了多行进行定义。在宏定义中,反斜杠 `\` 必须是宏定义的最后一个字符,且不能有空格或注释跟随。使用 `\` 进行续行时,注意保持代码的可读性,并确保每个行末都有 `\`。

探究PHP中define函数的重要性与作用探究PHP中define函数的重要性与作用Mar 19, 2024 pm 12:12 PM

PHP中define函数的重要性与作用1.define函数的基本介绍在PHP中,define函数是用来定义常量的关键函数,常量在程序运行过程中不会改变其值。利用define函数定义的常量,在整个脚本中均可被访问,具有全局性。2.define函数的语法define函数的基本语法如下:define("常量名称","常量值&qu

define怎么定义条件编译define怎么定义条件编译Oct 11, 2023 pm 01:20 PM

define定义条件编译可以使用 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif` 预处理指令来实现。

如何在Zend框架中使用ACL(Access Control List)进行权限控制如何在Zend框架中使用ACL(Access Control List)进行权限控制Jul 29, 2023 am 09:24 AM

如何在Zend框架中使用ACL(AccessControlList)进行权限控制导言:在一个Web应用程序中,权限控制是至关重要的一项功能。它可以确保用户只能访问其有权访问的页面和功能,并防止未经授权的访问。Zend框架提供了一种方便的方法来实现权限控制,即使用ACL(AccessControlList)组件。本文将介绍如何在Zend框架中使用ACL

使用 gcc (mingw32) 编译带有静态库的 DLL使用 gcc (mingw32) 编译带有静态库的 DLLFeb 09, 2024 am 10:00 AM

我有一个由外部工具(即cgo)生成的静态库,我们将其称为libsecondary.a。我想生成一个动态库,同时包含“libsecondary.a”作为依赖项,我在libsecondary.h中导出一个名为onprocessinit()的函数,并在dll_process_attach事件上调用它。我尝试生成共享库,但似乎无法使用x86_64-w64-mingw32-共享-l。-lsecondary-static-libgcc-static-libstdc++-static.\d

PHP实现框架:Zend Framework入门教程PHP实现框架:Zend Framework入门教程Jun 19, 2023 am 08:09 AM

PHP实现框架:ZendFramework入门教程ZendFramework是PHP开发的一种开源网站框架,目前由ZendTechnologies维护,ZendFramework采用了MVC设计模式,提供了一系列可重用的代码库,服务于实现Web2.0应用程序和Web服务。ZendFramework深受PHP开发者的欢迎和推崇,拥有广泛

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),