debug_zval_dump
debug_zval_dump(mixed $value): void
转储变量的 zval 内部细节(类型、值与引用计数),是观察 PHP 写时复制机制的调试窗口。
C C23
#include <stdio.h>
int main(void) {
// C 严格值语义:拷贝就是新的存储单元
int x = 1;
int y = x; // 值拷贝
int *px = &x, *py = &y;
printf("copied ints share storage: %s\n", px == py ? "true" : "false");
// 数组退化为指针:传参是对原存储的别名
int arr[2] = {1, 2};
int *pa = arr;
printf("array alias shares storage: %s\n", pa == (int *)&arr ? "true" : "false");
return 0;
}
copied ints share storage: false array alias shares storage: true
C++ C++26
#include <iostream>
int main() {
int x = 1;
int y = x; // 值拷贝:新存储
int &r = x; // 引用:同一单元的另一个名字
std::cout << "copied ints share storage: " << (&x == &y ? "true" : "false") << "\n";
std::cout << "reference shares storage: " << (&r == &x ? "true" : "false") << "\n";
return 0;
}
copied ints share storage: false reference shares storage: true
C# v11.0.100
// C# 通过 struct/class 行为观察值语义 vs 引用语义
var a = new Counter { N = 1 };
var b = a; // class:拷贝引用,背后是同一对象
b.N = 42;
Console.WriteLine($"reference copy sees the change: {a.N == 42}");
var s1 = (N: 1, Tag: "t");
var s2 = s1; // 元组(struct):拷贝值
s2.N = 42;
Console.WriteLine($"value copy sees the change: {s1.N == 42}");
class Counter
{
public int N;
}
reference copy sees the change: True value copy sees the change: False
Go v1.27.4
package main
import "fmt"
func mutateInt(n int) { n = 42 } // int 传参即拷贝
func mutateSlice(s []int) { s[0] = 42 } // 切片头拷贝,底层数组共享
func main() {
x := 1
mutateInt(x)
fmt.Println("int passed by value:", x == 1)
nums := []int{1, 2}
mutateSlice(nums)
fmt.Println("slice shares the backing array:", nums[0] == 42)
}
int passed by value: true slice shares the backing array: true
Java v27.0.1
public class Main {
static void mutateInt(int n) { n = 42; } // int 传参即拷贝
static void mutate(StringBuilder sb) { sb.append("!"); } // 引用拷贝,对象共享
public static void main(String[] args) {
int x = 1;
mutateInt(x);
System.out.println("int passed by value: " + (x == 1));
StringBuilder sb = new StringBuilder("hi");
mutate(sb);
System.out.println("object reference shares the instance: " + sb.toString().equals("hi!"));
}
}
int passed by value: true object reference shares the instance: true
JavaScript v26.13.1
let scalar = 1;
let copy = scalar; // 原始值按值拷贝
copy = 42;
console.log("scalar copy sees the change:", scalar === 1);
const obj = { n: 1 };
const ref = obj; // 对象按引用拷贝:同一实例
ref.n = 42;
console.log("object alias sees the change:", obj.n === 42);
scalar copy sees the change: true object alias sees the change: true
PHP v8.6.0
$a = range(1, 2); // 运行时构造:字面量数组的 refcount 标记会令人困惑
debug_zval_dump($a); // refcount(2):$a 本身 + 按值传参临时加一
$b = $a; // 赋值只把引用计数 +1,尚未复制数据(写时复制)
debug_zval_dump($a); // refcount(3):$a、$b、传参
$b[] = 3; // 对 $b 写入触发真实拷贝;$a 的引用计数回落
debug_zval_dump($a); // refcount(2):$b 已分离
array(2) packed refcount(2){
[0]=>
int(1)
[1]=>
int(2)
}
array(2) packed refcount(3){
[0]=>
int(1)
[1]=>
int(2)
}
array(2) packed refcount(2){
[0]=>
int(1)
[1]=>
int(2)
}
Python v3.15.2
def observe() -> None:
# 列表是引用:赋值即别名,指向同一对象
a = [1, 2]
b = a
b.append(3)
print("list alias sees the change:", a == [1, 2, 3])
print("same identity:", a is b)
# 整数不可变:重新绑定名字不会触碰旧值
x = 1
y = x
y = 42
print("int rebind sees the change:", x == 1)
observe()
list alias sees the change: True same identity: True int rebind sees the change: True
Rust v1.99.1
fn main() {
// i32 是 Copy 类型:普通值拷贝,原变量仍可用
let n1: i32 = 1;
let n2 = n1;
println!("Copy type keeps the original usable: {}", n1 == 1 && n2 == 1);
// String 是移动语义:赋值转移所有权(编译期规则)
let s1 = String::from("hi");
let s2 = s1; // 此后 s1 不可再用
let s3 = s2.clone(); // clone:独立分配的深拷贝
println!("clone is a new allocation: {}", !std::ptr::eq(s2.as_ptr(), s3.as_ptr()));
// println!("{}", s1); // 编译不过:值已被移动——借用检查器就是护栏
}
Copy type keeps the original usable: true clone is a new allocation: true
TypeScript v26.13.1
let scalar: number = 1;
let copy: number = scalar; // 原始值按值拷贝
copy = 42;
console.log("scalar copy sees the change:", scalar === 1);
const obj: { n: number } = { n: 1 };
const ref: { n: number } = obj; // 对象别名指向同一实例
ref.n = 42;
console.log("object alias sees the change:", obj.n === 42);
scalar copy sees the change: true object alias sees the change: true
语言差异说明
C:无引用计数概念——演示值语义观察惯用法:拷贝后地址不同(值语义)、数组退化为指针后共享存储(别名)。
C++:同 C,加引用 int& 与取地址比较:引用与原变量同址、拷贝不同址。
C#:无 refcount 观察窗口——用 class(引用共享)与 tuple/struct(值拷贝)的副作用差异演示两种语义。
Go:函数传参即拷贝——int 拷贝后外部不变,slice 拷贝的是头部、底层数组共享,注释点明这一 Go 特有模型。
Java:基本类型按值、对象按引用——int 传参后外部不变,StringBuilder 传参改动对外可见。
JavaScript:原始值按值拷贝、对象按引用共享别名,用「改动是否可见」做观察。
PHP:debug_zval_dump 是 PHP 特有的 zval 内部观察工具,其他语言没有对应概念——它打印变量的类型/值/refcount/is_ref,是理解「PHP 赋值是引用计数+写时复制」的实验台;输出里 refcount 比可见引用数多 1 来自传参本身;字面量字符串显示 interned(编译期常量无计数);本条目用 range() 运行时构造数组避开字面量数组的常量缓存干扰,得到干净的 2→3→2 序列:赋值只 +1 不复制、写入才触发真实拷贝(copy-on-write)——这正是 PHP 数组「赋值便宜」的原理;仅限调试使用。
Python:无 refcount 观察窗口(sys.getrefcount 数字受实现细节影响不可靠)——用 is 身份比较与「改动是否可见」演示引用语义 vs 不可变值语义。
Rust:所有权模型没有引用计数泄漏到运行时——演示 Copy 类型(值拷贝)与 move/clone(所有权转移与深拷贝),借用检查在编译期拦截悬垂使用。
TypeScript:同 JS,原始值按值、对象按引用。