php鐨勫彉閲忎互鍙婂紩鐢ㄥ彉閲忕殑宸ヤ綔鍘熺悊

1锛屼粈涔堟槸寮曠敤鍙橀噺锛熷湪php褰撲腑锛岀敤浠涔堢鍙峰畾涔夊紩鐢ㄥ彉閲
鍦≒HP涓紩鐢ㄦ剰鍛崇潃涓嶅悓鐨勫悕瀛楄闂悓涓涓彉閲忓唴瀹癸紱&

璇︾粏瑙f瀽

php寮曠敤鍙橀噺鐨勫伐浣滃師鐞
璇磋捣鏉hp寮曠敤鍙橀噺锛屼笉寰椾笉璇磒hp鐨勫彉閲忋

姣忎釜php鍙橀噺瀛樺湪涓涓彨”zval”鐨勫彉閲忓鍣ㄤ腑锛屼竴涓獄val鍙橀噺瀹瑰櫒锛岄櫎浜嗗寘鍚彉閲忕殑绫诲瀷鍜屽硷紝杩樺寘鎷袱涓瓧鑺傜殑棰濆淇℃伅銆傜涓涓槸”is_ref”锛屾槸涓猙ool鍊硷紝鐢ㄦ潵鏍囪瘑杩欎釜鍙橀噺鏄惁鏄睘浜庡紩鐢ㄩ泦鍚堬紱鍙﹀涓涓槸”refcount”,鐢ㄤ互琛ㄧず鎸囧悜杩欎釜zval鍙橀噺瀹瑰櫒鐨勫彉閲(涔熺О绗﹀彿鍗硈ymbol)涓暟

鎯宠鏌ョ湅杩欎袱涓肩殑璇濓紝鍙互瀹夎涓涓媝hp鐨刋DEBUG锛岀劧鍚庝娇鐢▁debug_debug_zval鍑芥暟锛屼紶鍏ュ彉閲忕殑鍙傛暟鍚嶅瓧灏卞彲浠ヤ簡锛屽

<?php
$a = array( 'one' );
$b =& $a;
xdebug_debug_zval( 'a' );
?>

鎵撳嵃鍑烘潵鐨勫兼槸
a:
(refcount=2, is_ref=1)
array (size=1)
0 => (refcount=2, is_ref=0)string ‘one’ (length=3)
unset($b)

class Person
{
    public $name ="zhangsan";
}
$p1 = new Person;
xdebug_debug_zval('p1');
$p2 = $p1;
xdebug_debug_zval('p1');
$p2->name = 'lisi';
xdebug_debug_zval('p1');

鎵撳嵃鍑烘潵鐨勫兼槸
p1:
(refcount=1, is_ref=0)
object(Person)[1]
public ‘name’ => (refcount=2, is_ref=0)string ‘zhangsan’ (length=8)
p1:
(refcount=2, is_ref=0)
object(Person)[1]
public ‘name’ => (refcount=2, is_ref=0)string ‘zhangsan’ (length=8)
p1:
(refcount=2, is_ref=0)
object(Person)[1]
public ‘name’ => (refcount=0, is_ref=0)string ‘lisi’ (length=4)

璇存槑瀵硅薄鏈韩灏辨槸寮曠敤浼犻掋

鍐欏嚭濡備笅绋嬪簭鐨勮緭鍑虹粨鏋

<?php
$data = ['a','b','c'];
foreach ($data as $key=>$val){
    $val = &$data[$key];
}

绋嬪簭姣忎竴娆″惊鐜悗锛$data鐨勫兼槸浠涔
绋嬪簭鎵ц瀹屾垚涔嬪悗锛屽彉閲$data鐨勫兼槸浠涔堛

绗竴娆″惊鐜,$key=0,$val=a,鐒跺悗$val=&$data[0],鎵浠$data杩樻槸[‘a’,’b’,’c’]
绗簩娆″惊鐜瘮杈冮噸瑕,$key=1,$val=b,杩欎釜鏃跺欏洜涓烘槸浼犻掑紩鐢紝鎵浠ュ氨鏀瑰彉浜$data[0]鐨勫硷紝鎵浠$data灏辨槸[‘b’,’b’,’c’]
鎵浠ョ涓夋鍚岀悊灏辨槸,鎵浠$data灏辨槸[‘b’,’c’,’c’]浜嗐

All posts

Other pages

鍙戣〃璇勮

鐢靛瓙閭欢鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢*鏍囨敞