鍙橀噺鐨勮寖鍥村嵆瀹冨畾涔夌殑涓婁笅鏂囪儗鏅紙涔熷氨鏄畠鐨勭敓鏁堣寖鍥达級銆傚ぇ閮ㄥ垎鐨 PHP 鍙橀噺鍙湁涓涓崟鐙殑鑼冨洿銆傝繖涓崟鐙殑鑼冨洿璺ㄥ害鍚屾牱鍖呭惈浜 include 鍜 require 寮曞叆鐨勬枃浠躲備緥濡
鍙橀噺鑼冨洿
鍙橀噺鐨勮寖鍥村嵆瀹冨畾涔夌殑涓婁笅鏂囪儗鏅紙涔熷氨鏄畠鐨勭敓鏁堣寖鍥达級銆傚ぇ閮ㄥ垎鐨 PHP 鍙橀噺鍙湁涓涓崟鐙殑鑼冨洿銆傝繖涓崟鐙殑鑼冨洿璺ㄥ害鍚屾牱鍖呭惈浜 include 鍜 require 寮曞叆鐨勬枃浠躲備緥濡傦細
<?php
$a = 1;
include 'b.inc';
?>
杩欓噷鍙橀噺 $a 灏嗕細鍦ㄥ寘鍚枃浠 b.inc 涓敓鏁堛備絾鏄紝鍦ㄧ敤鎴疯嚜瀹氫箟鍑芥暟涓紝涓涓眬閮ㄥ嚱鏁拌寖鍥村皢琚紩鍏ャ備换浣曠敤浜庡嚱鏁板唴閮ㄧ殑鍙橀噺鎸夌己鐪佹儏鍐靛皢琚檺鍒跺湪灞閮ㄥ嚱鏁拌寖鍥村唴銆備緥濡傦細
<?php
$a = 1; /* global scope */
function Test()
{
echo $a; /* reference to local scope variable */
}
Test();
?>
杩欎釜鑴氭湰涓嶄細鏈変换浣曡緭鍑猴紝鍥犱负 echo 璇彞寮曠敤浜嗕竴涓眬閮ㄧ増鏈殑鍙橀噺 $a锛岃屼笖鍦ㄨ繖涓寖鍥村唴锛屽畠骞舵病鏈夎璧嬪笺備綘鍙兘娉ㄦ剰鍒 PHP 鐨勫叏灞鍙橀噺鍜 C 璇█鏈変竴鐐圭偣涓嶅悓锛屽湪 C 璇█涓紝鍏ㄥ眬鍙橀噺鍦ㄥ嚱鏁颁腑鑷姩鐢熸晥锛岄櫎闈炶灞閮ㄥ彉閲忚鐩栥傝繖鍙兘寮曡捣涓浜涢棶棰橈紝鏈変簺浜哄彲鑳戒笉灏忓績灏辨敼鍙樹簡涓涓叏灞鍙橀噺銆侾HP 涓叏灞鍙橀噺鍦ㄥ嚱鏁颁腑浣跨敤鏃跺繀椤荤敵鏄庝负global銆
global 鍏抽敭瀛
棣栧厛锛屼竴涓娇鐢 global 鐨勪緥瀛愶細
Example #1 浣跨敤 global
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
浠ヤ笂鑴氭湰鐨勮緭鍑哄皢鏄3鈥濄傚湪鍑芥暟涓敵鏄庝簡鍏ㄥ眬鍙橀噺 $a 鍜 $b锛屼换浣曞彉閲忕殑鎵鏈夊紩鐢ㄥ彉閲忛兘浼氭寚鍚戝埌鍏ㄥ眬鍙橀噺銆傚浜庝竴涓嚱鏁拌兘澶熺敵鏄庣殑鍏ㄥ眬鍙橀噺鐨勬渶澶т釜鏁帮紝PHP 娌℃湁闄愬埗銆
鍦ㄥ叏灞鑼冨洿鍐呰闂彉閲忕殑绗簩涓姙娉曪紝鏄敤鐗规畩鐨 PHP 鑷畾涔 $GLOBALS 鏁扮粍銆傚墠闈㈢殑渚嬪瓙鍙互鍐欐垚锛
Example #2 浣跨敤 $GLOBALS 鏇夸唬 global
<?php
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
$GLOBALS 鏄竴涓叧鑱旀暟缁勶紝姣忎竴涓彉閲忎负涓涓厓绱狅紝閿悕瀵瑰簲鍙橀噺鍚嶏紝鍊煎搴斿彉閲忕殑鍐呭銆$GLOBALS 涔嬫墍浠ュ湪鍏ㄥ眬鑼冨洿鍐呭瓨鍦紝鏄洜涓 $GLOBALS 鏄竴涓秴鍏ㄥ眬鍙橀噺銆備互涓嬭寖渚嬫樉绀轰簡瓒呭叏灞鍙橀噺鐨勭敤澶勶細
Example #3 婕旂ず瓒呭叏灞鍙橀噺鍜屼綔鐢ㄥ煙鐨勪緥瀛
<?php
function test_global()
{
// 澶у鏁扮殑棰勫畾涔夊彉閲忓苟涓 "super"锛屽畠浠渶瑕佺敤 'global' 鍏抽敭瀛楁潵浣垮畠浠湪鍑芥暟鐨勬湰鍦板尯鍩熶腑鏈夋晥銆
global $HTTP_POST_VARS;
echo $HTTP_POST_VARS['name'];
// Superglobals 鍦ㄤ换浣曡寖鍥村唴閮芥湁鏁堬紝瀹冧滑骞朵笉闇瑕 'global' 澹版槑銆係uperglobals 鏄湪 PHP 4.1.0 寮曞叆鐨勩
echo $_POST['name'];
}
?>
浣跨敤闈欐佸彉閲
鍙橀噺鑼冨洿鐨勫彟涓涓噸瑕佺壒鎬ф槸闈欐佸彉閲忥紙static variable锛夈傞潤鎬佸彉閲忎粎鍦ㄥ眬閮ㄥ嚱鏁板煙涓瓨鍦紝浣嗗綋绋嬪簭鎵ц绂诲紑姝や綔鐢ㄥ煙鏃讹紝鍏跺煎苟涓嶄涪澶便傜湅鐪嬩笅闈㈢殑渚嬪瓙锛
Example #4 婕旂ず闇瑕侀潤鎬佸彉閲忕殑渚嬪瓙
<?php
function Test()
{
$a = 0;
echo $a;
$a++;
}
?>
鏈嚱鏁版病浠涔堢敤澶勶紝鍥犱负姣忔璋冪敤鏃堕兘浼氬皢 $a 鐨勫艰涓 0 骞惰緭鍑 "0"銆傚皢鍙橀噺鍔犱竴鐨 $a++ 娌℃湁浣滅敤锛屽洜涓轰竴鏃﹂鍑烘湰鍑芥暟鍒欏彉閲 $a 灏变笉瀛樺湪浜嗐傝鍐欎竴涓笉浼氫涪澶辨湰娆¤鏁板肩殑璁℃暟鍑芥暟锛岃灏嗗彉閲 $a 瀹氫箟涓洪潤鎬佺殑锛
Example #5 浣跨敤闈欐佸彉閲忕殑渚嬪瓙
<?php
function test()
{
static $a = 0;
echo $a;
$a++;
}
?>
鐜板湪锛屽彉閲$a鍦ㄧ涓璋冪敤test()鏃惰鍒濆鍖栵紝姣忔璋冪敤 test() 鍑芥暟閮戒細杈撳嚭 $a 鐨勫煎苟鍔犱竴銆
闈欐佸彉閲忎篃鎻愪緵浜嗕竴绉嶅鐞嗛掑綊鍑芥暟鐨勬柟娉曘傞掑綊鍑芥暟鏄竴绉嶈皟鐢ㄨ嚜宸辩殑鍑芥暟銆傚啓閫掑綊鍑芥暟鏃惰灏忓績锛屽洜涓哄彲鑳戒細鏃犵┓閫掑綊涓嬪幓銆傚繀椤荤‘淇濇湁鍏呭垎鐨勬柟娉曟潵涓閫掑綊銆備竴涓嬭繖涓畝鍗曠殑鍑芥暟閫掑綊璁℃暟鍒 10锛屼娇鐢ㄩ潤鎬佸彉閲 $count 鏉ュ垽鏂綍鏃跺仠姝細
Example #6 闈欐佸彉閲忎笌閫掑綊鍑芥暟
<?php
function test()
{
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
test();
}
$count--;
}
?>
Note:
闈欐佸彉閲忓彲浠ユ寜鐓т笂闈㈢殑渚嬪瓙澹版槑銆傚鏋滃湪澹版槑涓敤琛ㄨ揪寮忕殑缁撴灉瀵瑰叾璧嬪间細瀵艰嚧瑙f瀽閿欒銆
Example #7 澹版槑闈欐佸彉閲
<?php
function foo(){
static $int = 0; // correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
$int++;
echo $int;
}
?>
鍏ㄥ眬鍜岄潤鎬佸彉閲忕殑寮曠敤
鍦 Zend 寮曟搸 1 浠o紝瀹冮┍鍔ㄤ簡 PHP4锛屽浜庡彉閲忕殑 static 鍜 global 瀹氫箟鏄互 references 鐨勬柟寮忓疄鐜扮殑銆備緥濡傦紝鍦ㄤ竴涓嚱鏁板煙鍐呴儴鐢 global 璇彞瀵煎叆鐨勪竴涓湡姝g殑鍏ㄥ眬鍙橀噺瀹為檯涓婃槸寤虹珛浜嗕竴涓埌鍏ㄥ眬鍙橀噺鐨勫紩鐢ㄣ傝繖鏈夊彲鑳藉鑷撮鏂欎箣澶栫殑琛屼负锛屽浠ヤ笅渚嬪瓙鎵婕旂ず鐨勶細
<?php
function test_global_ref() {
global $obj;
$obj = &new stdclass;
}
function test_global_noref() {
global $obj;
$obj = new stdclass;
}
test_global_ref();
var_dump($obj);
test_global_noref();
var_dump($obj);
?>
鎵ц浠ヤ笂渚嬪瓙浼氬鑷村涓嬭緭鍑猴細
NULL
object(stdClass)(0) {
}
绫讳技鐨勮涓轰篃閫傜敤浜 static 璇彞銆傚紩鐢ㄥ苟涓嶆槸闈欐佸湴瀛樺偍鐨勶細
<?php
function &get_instance_ref() {
static $obj;
echo 'Static object: ';
var_dump($obj);
if (!isset($obj)) {
// 灏嗕竴涓紩鐢ㄨ祴鍊肩粰闈欐佸彉閲
$obj = &new stdclass;
}
$obj->property++;
return $obj;
}
function &get_instance_noref() {
static $obj;
echo 'Static object: ';
var_dump($obj);
if (!isset($obj)) {
// 灏嗕竴涓璞¤祴鍊肩粰闈欐佸彉閲
$obj = new stdclass;
}
$obj->property++;
return $obj;
}
$obj1 = get_instance_ref();
$still_obj1 = get_instance_ref();
echo " ";
$obj2 = get_instance_noref();
$still_obj2 = get_instance_noref();
?>
鎵ц浠ヤ笂渚嬪瓙浼氬鑷村涓嬭緭鍑猴細
Static object: NULL
Static object: NULL
Static object: NULL
Static object: object(stdClass)(1) {
["property"]=>
int(1)
}
涓婁緥婕旂ず浜嗗綋鎶婁竴涓紩鐢ㄨ祴鍊肩粰涓涓潤鎬佸彉閲忔椂锛岀浜屾璋冪敤 &get_instance_ref() 鍑芥暟鏃跺叾鍊煎苟娌℃湁琚浣忋
浠ヤ笂灏辨槸PHP鍙橀噺涓殑鍙橀噺鑼冨洿浠嬬粛鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒