class a {
public $test="msg1";
}
$t1 = new a;
echo "echo1: After Instantiation :<br/>";
xdebug_debug_zval('t1');echo "<br/><br/>";
$t2 = $t1;
echo 'echo2: After assigning $t1 to $t2 :<br/>';
xdebug_debug_zval('t2');echo "<br/><br/>";
$t1->test="msg2";
echo 'echo3: After assigning $t1->test = "msg2" :<br/>';
xdebug_debug_zval('t1');echo "<br/>";
xdebug_debug_zval('t2');echo "<br/><br/>";
$t2->test="msg3";
echo 'echo4: After assigning $t2->test="msg3" :<br/>';
xdebug_debug_zval('t1');echo "<br/>";
xdebug_debug_zval('t2');echo "<br/><br/>";
$t2->test2 = "c*ap!";
echo 'echo5: After injecting $test2 to $t2 :<br/>';
xdebug_debug_zval('t1');echo "<br/>";
xdebug_debug_zval('t2');echo "<br/><br/>";
輸出:爲什麼C.O.W在'寫入物業'/'將物業注入物品中'時不會發生?
ECHO1:實例化後:
T1:(引用計數= 1,is_ref = 0)= A類{公共$測試=(引用計數= 2,is_ref = 0)= 'MSG1'}echo2的:分配$ t1到$ t2以後:
T2:(引用計數= 2,is_ref = 0)= A類{公共$測試=(引用計數= 2,is_ref = 0)= 'msg1'}echo3:分配$ t後1-> test =「msg2」:
t1:(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg2'}
t2:(refcount = 2,is_ref = 0)= A類{公共$測試=(引用計數= 1,is_ref = 0)= 'MSG2'}echo4:分配$ T2->測試= 「消息3」 之後:
t1:(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg3'}
t2:(refcount = 2,is_ref = 0)= class a {公共$ test =(refcount = 1,is_ref = 0)='msg3'}echo5:向$ t2注入$ test2之後:
t1:(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg3'; public $ test2 =(refcount = 1,is_ref = 0)='c ap!' }
t2:(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg3'; public $ test2 =(refcount = 1,is_ref = 0)='c ap!' }
忽略echo1
& echo2
因爲這樣:What is exactly happening when instantiating with 'new'? &預期行爲。
考慮echo3
:
echo3:分配$ T1-後>測試= 「MSG2」:
T1:(引用計數= 2,is_ref = 0)= A類{公共$測試=(引用計數t2 =(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg2'}
這是可以理解的,因爲我只是更改$t1->test
變量而沒有直接更改爲&t2->test
。
考慮echo4
,其中一個直接改變到$t2->test
完成:
echo4:分配$ T2-後>測試= 「消息3」:
T1:(引用計數= 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg3'}
t2:(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0 )='msg3'}
沒有COW發生!並且該變更也反映爲$t1
,即使未設置is_ref
也是如此。
考慮echo5
,其中變量$test2
被注入$t2
:
echo5:
T1:(引用計數= 2,is_ref = 0)= A類{公共注入$ TEST2到$ t2以後$ test =(refcount = 1,is_ref = 0)='msg4'; public $ test2 =(refcount = 1,is_ref = 0)='c ap!' }
t2:(refcount = 2,is_ref = 0)= class a {public $ test =(refcount = 1,is_ref = 0)='msg4'; public $ test2 =(refcount = 1,is_ref = 0)='c ap!' }
再次,沒有C.O.W發生!並且該變更也反映爲$t1
,即使未設置is_ref
也是如此。
Why is this behaviour!?
正在看........ – ThinkingMonkey 2012-01-01 10:55:42
好吧。將做..... – ThinkingMonkey 2012-01-01 11:10:17
完成分配後,因爲兩者都指向同一個zval容器,所以變更得到反映。具有「引用賦值」的要點是,如果,$ t1 = new a; $ t2 =&$ t1,$ t2 = new b;現在,$ t1和$ t2都將指向b類對象的zval容器? – ThinkingMonkey 2012-01-01 11:13:17