Table of Contents
深入理解PHP内核(五)变量及数据类型-变量的结构和类型,深入理解内核
Home php教程 php手册 深入理解PHP内核(五)变量及数据类型-变量的结构和类型,深入理解内核

深入理解PHP内核(五)变量及数据类型-变量的结构和类型,深入理解内核

Jun 13, 2016 am 08:43 AM
php

深入理解PHP内核(五)变量及数据类型-变量的结构和类型,深入理解内核

原文链接:http://www.orlion.ga/238/

编程语言的类型可以分为强类型和弱类型两种,PHP是弱类型语言,但是C语言是强类型语言。在官网PHP实现内部,所有变量使用同一种数据结构(zval)来保存,这个结构表示PHP中的各种数据类型,它不仅包含变量的值,也包含变量的类型。这就是PHP弱类型的核心。

那zval结构是如何实现弱类型的呢?

 

一、PHP变量类型及存储结构

 

    PHP在声明和使用变量的时候,并不需要指明其数据类型,PHP是弱类型语言,但是PHP有类型,PHP有八中数据类型,可以分成三类:标量类型:boolean、integer、float(double) string;复合类型:array、object;特殊类型:resource、NULL

    C语言是如何实现PHP中的弱类型的呢?

 

1、变量存储结构

    

    变量的值存储到如下所示的zval结构体中,zval结构体定义在Zend/zend.h文件,其结构如下:

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;
};
Copy after login

zval结构体中有四个字段,其含义分别为:

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

在PHP5.3之后,引入了新的垃圾回收机制,引用计数和引用的字段名改为refcout_gc和is_ref_gc在此之前为refcount和is_ref.

变量的值存储在另外一个结构体zvalue_value,值存储见下面的介绍。

2、变量类型

zval结构体的type字段就是实现弱类型最关键的字段了,type的值可以为:IS_NULL、IS_BOOL、IS_LONG、IS_DOUBLE、IS_STRING、IS_OBJECT和IS_RESOURCE之一。除此之外和它们定义在一起的类型还有IS_CONSTANT、IS_CONSTANT_ARRAY。

二、变量的值存储

前面提到变量的值存储在zvalue_value联合体中,结构体定义如下:

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;
Copy after login

各种类型的数据会使用不同的方法来进行变量值的存储,其对应赋值方式如下:

一般类型:

变量类型
boolean ZVAL_BOOL

布尔型/整型的变量值存储于(zval).value.lval中,其类型也会

以相应的IS_*进行存储

integer ZVAL_LONG
float ZVAL_DOUBLE Z_TYPE_P(z)=IS_BOOL/LONG;Z_LVAL_P(z)=(b)!=0;
null ZVAL_NULL

NULL值的变量值不需要存储,只需要把(zval).type标为IS_NUL

Z_TYPE_P(z)=IS_NULL;

resource ZVAL_RESOURCE

资源类型的存储与其他一般变量无异,但其初始化及存储实现则不

同Z_TYPE_P(z) = IS_RESOURCE; Z_LVAL_P(z) = 1;

字符串

字符串的类型标示和其他数据类型一样,不过在存储字符串时多了一个字符串长度的字段。

struct {
    char *val;
    int len;
} str;
Copy after login

(存储字符串长度是因为字符串的操作十分频繁,有利于节省时间,是空间换时间的做法)

数组Array

数组是PHP中最常用也是最强大变量类型。数组的值存储在zvalue_value.ht字段中,它是一个HashTable类型的数据。PHP数组使用哈希表来存储关联数据。PHP的哈希表实现中使用了两个数据结构HashTable和Bucket。PHP所有的工作都是由哈希表实现。

对象Object

PHP的对象是一种复合型的数据,使用一种zend_object_value的结构体来存放,其定义如下

typedef struct _zend_object_value {
    zend_object_handle handle;  //  unsigned int„类型,EG(objects_store).object_buckets的索引
    zend_object_handlers *handlers;
} zend_object_value;
Copy after login

PHP的对象只有在运行时才会被创建,前面介绍了EG宏,这是一个全局结构体由于保存在运行时的数据。其中就包括了用来保存所有被创建的对象的对象池,EG(objects_store),而object对象值内容的zend_object_handle域就是当前对象在对象池中所在的索引,handlers字段则是将对象进行操作时的处理函数保存起来。

    

    PHP的弱变量容器的实现方式是兼容并包的形式体现。

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles