在php中取消设置变量 [英] unset variable in php
问题描述
我刚刚通过 php 手册阅读了有关未设置变量的信息.
php 手册说unset() 销毁指定的变量"
这个定义看起来很完美,直到我遇到了静态变量......如果函数内部的静态变量是 unset(),则 unset() 只会在函数其余部分的上下文中销毁该变量.随后的调用将恢复变量的先前值."
这个定义对我来说似乎不是一个好定义,至少,因为销毁变量"意味着变量不再与该内存位置相关联.
有没有其他人认为更好的定义是unset() 使变量超出当前范围"?我的意思是,与其指向生命周期,不如在这里使用词范围?
让我们考虑一下功能:
function foo() {静态 $bar;$bar++;未设置($bar);}富();//静态 $bar 为 1富();//静态 $bar 是 2
函数编译为:
<前>函数名:foo操作数:11编译变量:!0 = $barline # * op fetch ext 返回操作数---------------------------------------------------------------------------------2 0 > EXT_NOP4 1 EXT_STMT2 FETCH_W 静态 $0 'bar'3 ASSIGN_REF !0, $05 4 EXT_STMT5 POST_INC ~1 !06 免费 ~16 7 EXT_STMT8 UNSET_VAR !07 9 EXT_STMT10 > 返回空一个变量实际上存在于对 foo()
的每个函数调用之外,并且在每次调用时,它都会被提取并且对它的引用被分配给 $bar
.其实和这个很像:
function foo() {全球 $bar;$bar++;未设置($bar);}
当您调用 unset()
时,您只会破坏您创建的引用,而不是基础值.
我没有确认,但我猜会发生这样的事情:
- 存储变量的底层表示(zval),以便其引用计数为 1.
- 当
foo()
被调用时,符号$bar
与这个 zval 相关联,它的引用计数增加到 2 并设置引用标志.立> - 当
unset
被调用时,zval 的引用计数减少到 1,引用标志可能被清除并且符号$bar
被删除.
请参阅引用计数基础知识.
I just read about unset variable through php manual.
The php manual says "unset() destroys the specified variables"
This def seems perfect until I came across static variable... "If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable. "
This definition doesn't seems a good one for me, at least, since "destroy the variable" implies that the variable is no longer associated with that memory location.
Does anyone else think a better definition would be "unset() makes the variable out of current scope"? I mean, rather than pointing towards lifetime, it's better to use word scope here?
Let's consider the function:
function foo() {
static $bar;
$bar++;
unset($bar);
}
foo(); //static $bar is 1
foo(); //static $bar is 2
The function compiles to:
function name: foo number of ops: 11 compiled vars: !0 = $bar line # * op fetch ext return operands --------------------------------------------------------------------------------- 2 0 > EXT_NOP 4 1 EXT_STMT 2 FETCH_W static $0 'bar' 3 ASSIGN_REF !0, $0 5 4 EXT_STMT 5 POST_INC ~1 !0 6 FREE ~1 6 7 EXT_STMT 8 UNSET_VAR !0 7 9 EXT_STMT 10 > RETURN null
A variable actually exists outside each function call to foo()
and, on each call, it's fetched and a reference to it is assigned to $bar
. In fact, it's very similar to this:
function foo() {
global $bar;
$bar++;
unset($bar);
}
When you call unset()
, you're only destroying the reference you created, not the underlying value.
I didn't confirm, but what I'd guess that happens is this:
- The underlying representation of the variabe (the zval) is stored so that its reference count is 1.
- When
foo()
is called, the symbol$bar
is associated with this zval, its reference count is increased to 2 and the reference flag is set. - When
unset
is called, the zval has its reference count decreased to 1, the reference flag is probably cleared and the symbol$bar
is removed.
这篇关于在php中取消设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!