unset()經常會被用到,用於銷燬指定的變量,但它有自己的行爲模式,如果不仔細的話可能會被中文解釋給迷惑:
先來看看官方文檔的說法:
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()是語言結構,不是函數,因此不能被函數變量調用,具體參照函數變量。
使用function_exists('unset')返回的false,以此證明unset並不是一個函數,所以無法使用$fun='unset';$fun()的方式調用unset()
It is possible to unset even object properties visible in current context.
//通用環境下unset可以銷燬對象或者對象的可見屬性(public)
It is not possible to unset $this inside an object method since PHP5
在php5之前unset無法銷燬對象中的$this方法
when using unset() on inaccessible object properties,the __unset() overloading method will be called,if declare.
當unset()無法銷燬對象中的屬性,例如私有屬性,保護屬性,那麼會自動加載對象中的__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()的行爲在函數內部可以根據你所指定銷燬的變量類型變化。
情況一:
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.
如果在函數內一個使用global使其全局化的變量,使用unset進行銷燬,那麼只有局部的變量會被銷燬,在調用環境的變量將會保留沒有使用unset()銷燬之前的調用的變量值。
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;
上面的例子會返回如下結果:可以看到函數內echo $foo會得到錯誤提示該變量沒有定義,因爲unset將$foo在函數內的局部變量銷燬了。
而外部調用環境的$foo仍保留着沒有被unset進行銷燬,上面官方描述上寫了調用環境的$foo將保留的是在使用unset()前的變量值,因此echo出bars,而不是bar。
to unset() a global variable inside of a function,then use the $GLOBALS array to do so.
如果要用unset()銷燬函數中的全局變量,應該使用$GLOBALS數組形式
function destroy_foo(){
unset($GLOBALS['foo']);
}
$foo = 'bar';
destroy_foo();
echo $foo;
the above example whill output: Notice: Undefined variable: foo in ...延伸:
這裏可以明確一點,函數內global修飾的變量$var會使其全局化,但和$GLOBALS['var']性質不同,雖然他們都是使用的外部調用環境的$var,但是函數內global $var裏保存的只是外部環境調用變量$val的一個指針或者同名引用,而$GLOBALS['var']是外部調用環境$var本身,因此unset在函數內對兩者進行銷燬會產生上面例子的不同的原因,前者銷燬的只是保存同名引用或者指針的變量$var,後者銷燬的是外部調用變量$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.
如果一個被引用的函數在函數內部使用unset()進行銷燬,那麼只有函數內部的局部變量被銷燬,而調用環境變量仍然會保留unset()之前被調用的值。
<?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
這個和上面global其實很相似,函數內部引用的變量$bar其實保存的是外部環境變量$bar的指針或者說外部變量$bar的引用,因此unset在函數內部銷燬的並不是外部環境調用的變量,因此外部環境調用的變量$bar還存在。
<?php
function fun1(){
static $count=0;
$count++;
echo "before:".$count." ";
$count='2';
echo "after".$count." ";
}
for($i=0;$i<5;$i++){
fun1();
echo "<br/>";
}
output:<?php
function fun1(){
static $count=0;
$count++;
echo "before:".$count." ";
unset($count);
$count='2';
echo "after".$count." ";
}
for($i=0;$i<5;$i++){
fun1();
echo "<br/>";
}
output: