佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 深入理解php中unset()

深入理解php中unset()

熱門(mén)標(biāo)簽:服務(wù)器配置 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云 電子圍欄 Linux服務(wù)器

概述

unset()經(jīng)常會(huì)被用到,用于銷毀指定的變量,但它有自己的行為模式,如果不仔細(xì)的話可能會(huì)被中文解釋給迷惑:

先來(lái)看看官方文檔的說(shuō)法:

unset  —— unset a given variable

void unset (mixed $var [,mixed $...]); 

parameters:

var:The variable to be unset.    //要unset的變量

...:Another variable ... //其他需要unset的變量

return Values:No value is returned.    //unset不返回值

Because this is a language construct and not a function,it cannot be called using variable functions.

//unset()是語(yǔ)言結(jié)構(gòu),不是函數(shù),因此不能被函數(shù)變量調(diào)用,具體參照函數(shù)變量。

使用function_exists('unset')返回的false,以此證明unset并不是一個(gè)函數(shù),所以無(wú)法使用$fun='unset';$fun()的方式調(diào)用unset()

It is possible to unset even object properties visible in current context.

//通用環(huán)境下unset可以銷毀對(duì)象或者對(duì)象的可見(jiàn)屬性(public)

It is not possible to unset $this inside an object method since PHP5

在php5之前unset無(wú)法銷毀對(duì)象中的$this方法

when using unset() on inaccessible  object properties,the __unset() overloading method will be called,if declare.

當(dāng)unset()無(wú)法銷毀對(duì)象中的屬性,例如私有屬性,保護(hù)屬性,那么會(huì)自動(dòng)加載對(duì)象中的__unset方法。

description:

unset() destroys the specified variables.     //unset()銷毀指定的變量

The behavior of unset() inside of a function can vary dependiing on what type of variable you are attempting to destroy.

//unset()的行為在函數(shù)內(nèi)部可以根據(jù)你所指定銷毀的變量類型變化。

變化情況

情況一:

if a globalized variable is unset() inside of a function,only the local variable is destroyed.The variable in the calling environment will retain the same value as before unset() was called.

如果在函數(shù)內(nèi)一個(gè)使用global使其全局化的變量,使用unset進(jìn)行銷毀,那么只有局部的變量會(huì)被銷毀,在調(diào)用環(huán)境的變量將會(huì)保留沒(méi)有使用unset()銷毀之前的調(diào)用的變量值。

the example:

?php
function destroy_foo() 
{
    global $foo;
    unset($foo);
}
$foo = 'bar';
destroy_foo();
echo $foo;
?>

the above example will output:bar

這是官方文檔的例子,可能這樣還是不太明顯,把上面的例子改成下面這樣,一切就很清晰了。

?php 
function des(){
	global $foo;
	$foo='bars';
	unset($foo);
	echo $foo;
}
$foo='bar';
echo "The globalized variable is unset() inside of a function:";
des();
echo "br/>";
echo "The variable in the calling environment:";
echo $foo;

上面的例子會(huì)返回如下結(jié)果:

可以看到函數(shù)內(nèi)echo $foo會(huì)得到錯(cuò)誤提示該變量沒(méi)有定義,因?yàn)閡nset將$foo在函數(shù)內(nèi)的局部變量銷毀了。

而外部調(diào)用環(huán)境的$foo仍保留著沒(méi)有被unset進(jìn)行銷毀,上面官方描述上寫(xiě)了調(diào)用環(huán)境的$foo將保留的是在使用unset()前的變量值,因此echo出bars,而不是bar。

to unset() a global variable inside of a function,then use the $GLOBALS array to do so.

如果要用unset()銷毀函數(shù)中的全局變量,應(yīng)該使用$GLOBALS數(shù)組形式

function destroy_foo(){
	unset($GLOBALS['foo']);
}
$foo = 'bar';
destroy_foo();
echo $foo;

the above example whill output: Notice: Undefined variable: foo in ...

延伸:

這里可以明確一點(diǎn),函數(shù)內(nèi)global修飾的變量$var會(huì)使其全局化,但和$GLOBALS['var']性質(zhì)不同,雖然他們都是使用的外部調(diào)用環(huán)境的$var,但是函數(shù)內(nèi)global $var里保存的只是外部環(huán)境調(diào)用變量$val的一個(gè)指針或者同名引用,而$GLOBALS['var']是外部調(diào)用環(huán)境$var本身,因此unset在函數(shù)內(nèi)對(duì)兩者進(jìn)行銷毀會(huì)產(chǎn)生上面例子的不同的原因,前者銷毀的只是保存同名引用或者指針的變量$var,后者銷毀的是外部調(diào)用變量$var本身。

情況二:

if a variable  that is PASSED BY REFERENCE is unset() inside of a function,only the local variable is destroyed.The variable in the calling environment will retain the same value as before unset() was called.

如果一個(gè)被引用的函數(shù)在函數(shù)內(nèi)部使用unset()進(jìn)行銷毀,那么只有函數(shù)內(nèi)部的局部變量被銷毀,而調(diào)用環(huán)境變量仍然會(huì)保留unset()之前被調(diào)用的值。

?php 
function foo($bar){
	unset($bar);
	$bar="blah";
}
$bar='something';
echo $bar."\n";
foo($bar);
echo $bar."\n";

The above example will output:something something

這個(gè)和上面global其實(shí)很相似,函數(shù)內(nèi)部引用的變量$bar其實(shí)保存的是外部環(huán)境變量$bar的指針或者說(shuō)外部變量$bar的引用,因此unset在函數(shù)內(nèi)部銷毀的并不是外部環(huán)境調(diào)用的變量,因此外部環(huán)境調(diào)用的變量$bar還存在。

情況三:

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.

如果是函數(shù)內(nèi)部的靜態(tài)變量使用unset(),unset()銷毀掉函數(shù)內(nèi)部的靜態(tài)變量,但是下次調(diào)用會(huì)恢復(fù)之前該靜態(tài)變量的值。
為了更明顯,下面的例子進(jìn)行對(duì)比,這里對(duì)官方的范例進(jìn)行了修改對(duì)比:

?php 
function fun1(){
	static $count=0;
	$count++;
	echo "before:".$count." ";
	$count='2';
	echo "after".$count." ";
}
for($i=0;$i5;$i++){
	fun1();
	echo "br/>";
}

output: 

下面使用unset:

?php 
function fun1(){
	static $count=0;
	$count++;
	echo "before:".$count." ";
	unset($count);
	$count='2';
	echo "after".$count." ";
}
for($i=0;$i5;$i++){
	fun1();
	echo "br/>";
}

output: 

兩個(gè)對(duì)比可以看出,unset只是將函數(shù)內(nèi)部的靜態(tài)變量進(jìn)行了銷毀,但沒(méi)有銷毀儲(chǔ)存在內(nèi)存里的該靜態(tài)變量的值,因?yàn)樵诤瘮?shù)內(nèi)部用static聲明的靜態(tài)變量存儲(chǔ)的僅僅是指向內(nèi)存存儲(chǔ)該靜態(tài)變量的一個(gè)指針,因此unset()銷毀的時(shí)候也僅僅是銷毀了該指針,但存儲(chǔ)在內(nèi)存里的靜態(tài)變量值沒(méi)有受到影響,因此再次調(diào)用該函數(shù)的時(shí)候,static該變量再次建立了與內(nèi)存中該變量的指針關(guān)系,而上一次調(diào)用時(shí)unset()之前的該變量值依舊存在,因此會(huì)恢復(fù)該變量上一次的值,因此$count進(jìn)行了遞增。

以上就是深入理解php中unset()的詳細(xì)內(nèi)容,更多關(guān)于php unset()的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • PHP unset函數(shù)原理及使用方法解析
  • php中用unset銷毀變量并釋放內(nèi)存
  • 關(guān)于php unset對(duì)json_encode的影響詳解
  • php使用unset()刪除數(shù)組中某個(gè)單元(鍵)的方法
  • PHP魔術(shù)方法__ISSET、__UNSET使用實(shí)例
  • PHP中isset()和unset()函數(shù)的用法小結(jié)
  • 解析PHP中的unset究竟會(huì)不會(huì)釋放內(nèi)存
  • php unset全局變量運(yùn)用問(wèn)題的深入解析
  • php中is_null,empty,isset,unset 的區(qū)別詳細(xì)介紹
  • PHP中使用unset銷毀變量并內(nèi)存釋放問(wèn)題
  • php中session_unset與session_destroy的區(qū)別分析
  • PHP中刪除變量時(shí)unset()和null的區(qū)別分析

標(biāo)簽:萍鄉(xiāng) 江蘇 蚌埠 衡水 廣元 棗莊 衢州 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《深入理解php中unset()》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    和政县| 辽阳市| 昭苏县| 分宜县| 嘉祥县| 咸丰县| 鹤岗市| 寿宁县| 明水县| 甘南县| 康马县| 芒康县| 滨州市| 新巴尔虎左旗| 黔江区| 库伦旗| 嘉祥县| 方山县| 阳春市| 新源县| 渑池县| 昭觉县| 新竹县| 繁峙县| 通河县| 临泉县| 阜城县| 兴国县| 乳山市| 建宁县| 东阳市| 中牟县| 乐陵市| 江山市| 上栗县| 陕西省| 永修县| 紫阳县| 盖州市| 绍兴县| 南漳县|