PHP Extension の書き方について調べていたのですが、refcount のところで
行き詰まりました。テストプログラムを書いてみたのですが、なぜこうなるのか、
理屈がよくわかりません。どなたかご教示いただけないでしょうか。
- TEST1 で refcount=0 はこれでいいの?。1 かと思っていた。
- TEST3 でいきなり refcount=2 になるの? 0+1=1 かと思っていた。
- TEST6 で refcount=4 になるのは、、、
php
1$ cat ref_test.php 2<?php 3echo 'PHP_VERSION = ' . PHP_VERSION . PHP_EOL; 4$a = "new string"; 5echo 'TEST1: after $a = "new string"' . PHP_EOL; 6xdebug_debug_zval('a', 'b', 'c'); 7$b = $a; 8echo 'TEST2: after $b = $a' . PHP_EOL; 9xdebug_debug_zval('a', 'b', 'c'); 10$c = &$a; 11echo 'TEST3: after $c = &$a' . PHP_EOL; 12xdebug_debug_zval('a', 'b', 'c'); 13$a = "another string"; 14echo 'TEST4: after $a = "another string"' . PHP_EOL; 15xdebug_debug_zval('a', 'b', 'c'); 16$c = "further string"; 17echo 'TEST5: after $c = "further string"' . PHP_EOL; 18xdebug_debug_zval('a', 'b', 'c'); 19echo 'TEST6: after dump($a, $b, $c)' . PHP_EOL; 20dump($a, $b, $c); 21 22function dump(&$x, &$y, &$z) { 23 xdebug_debug_zval('x', 'y', 'z'); 24} 25
bash
1$ php ref_test.php 2PHP_VERSION = 7.1.5 3TEST1: after $a = "new string" 4a: (refcount=0, is_ref=0)='new string' 5b: (refcount=0, is_ref=0)=*uninitialized* 6c: (refcount=0, is_ref=0)=*uninitialized* 7TEST2: after $b = $a 8a: (refcount=0, is_ref=0)='new string' 9b: (refcount=0, is_ref=0)='new string' 10c: (refcount=0, is_ref=0)=*uninitialized* 11TEST3: after $c = &$a 12a: (refcount=2, is_ref=1)='new string' 13b: (refcount=0, is_ref=0)='new string' 14c: (refcount=2, is_ref=1)='new string' 15TEST4: after $a = "another string" 16a: (refcount=2, is_ref=1)='another string' 17b: (refcount=0, is_ref=0)='new string' 18c: (refcount=2, is_ref=1)='another string' 19TEST5: after $c = "further string" 20a: (refcount=2, is_ref=1)='further string' 21b: (refcount=0, is_ref=0)='new string' 22c: (refcount=2, is_ref=1)='further string' 23TEST6: after dump($a, $b, $c) 24x: (refcount=4, is_ref=1)='further string' 25y: (refcount=2, is_ref=1)='new string' 26z: (refcount=4, is_ref=1)='further string' 27
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。