downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

doubleval> <boolval
[edit] Last updated: Fri, 17 May 2013

view this page in

debug_zval_dump

(PHP 4 >= 4.2.0, PHP 5)

debug_zval_dump内部的な Zend の値を表す文字列をダンプする

説明

void debug_zval_dump ( mixed $variable )

内部的な Zend の値を表す文字列をダンプします。

パラメータ

variable

評価される変数

返り値

値を返しません。

例1 debug_zval_dump() の例

<?php
$var1 
'Hello World';
$var2 '';

$var2 =& $var1;

debug_zval_dump(&$var1);
?>

上の例の出力は以下となります。

&string(11) "Hello World" refcount(3)

注意: refcount に注意

この関数によって返される refcount の値は、 特定の状況下では自明ではありません。例えば、とある開発者は上記の例で refcount2 を示すと予想したかも知れません。 実際に debug_zval_dump() がコールされるとき、 3 番目の参照が生成されます。

この動作は、変数が debug_zval_dump() に参照渡しされない場合、よりその度合いを増します。 例えば、上記の例を僅かに修正したバージョンを考えてみます。

<?php
$var1 
'Hello World';
$var2 '';

$var2 =& $var1;

debug_zval_dump($var1); // この時、参照で渡されない
?>

上の例の出力は以下となります。

string(11) "Hello World" refcount(1)

なぜ refcount(1) でしょうか? なぜなら関数がコールされるとき、 $var1 のコピーが作成されるためです。

この関数は、refcount1 の変数が渡される (コピーもしくは値渡し) とき、より 混乱させます。

<?php
$var1 
'Hello World';

debug_zval_dump($var1);
?>

上の例の出力は以下となります。

string(11) "Hello World" refcount(2)

ここで refcount2 は自明ではありません。特に上記の例を考える場合はそうです。 では、何が起こっているのでしょうか ?

変数が単一の参照 ( debug_zval_dump() の引数として使用される前の $var1) を持つ場合、PHP のエンジンは関数に渡される様式を最適化します。内部的に PHP は参照 (refcount がこの関数のスコープのために増加されます) のように $var1 を扱い、参照渡しされた変数に 書き込みが発生するかどうかの警告を伴ってコピーが作成されます。 ただし、書き込みの瞬間だけです。これは "copy on write" として知られます。

そのため、もし debug_zval_dump() がただ一つのパラメータ (もしくはない) に書き込みが発生した場合、コピーが作成されます。 その時までパラメータは参照を保持し、関数呼び出しのスコープに対して refcount2 にインクリメントされる原因になります。

参考



add a note add a note User Contributed Notes debug_zval_dump - [2 notes]
up
6
AmberBlackthorn
2 years ago
The add of "Call-time pass-by-reference" as E_DEPRECATED makes it impossible to get the real refcount without getting an error, since <?php error_reporting(E_ALL ^ E_DEPRECATED); ?> and even <?php @debug_zval_dump(&$foo); ?> doesn't change anything.
up
1
theriault
10 months ago
You can return a consistent refcount value for any variable (and it will work in PHP 5.3+ without problems) using the following function. 0 is returned if the variable passed has no references outside itself or the variable doesn't exist (use isset in conjunction with this function if you need to distinguish between the two).
<?php
/**
* Return the reference count of a variable.
* Returns 0 if a variable has no reference other than itself or doesn't exist.
*/
function refcount(&$var) {
   
ob_start();
   
debug_zval_dump(array(&$var));
    return
preg_replace("/^.+?refcount\((\d+)\).+$/ms", '$1', substr(ob_get_clean(), 24), 1) - 4;
}

# Example
$a = 34;
refcount($a) == 0;

$b = &$a;
refcount($a) == 1;
?>

Using the above function as a dependency, you can determine if two variables reference the same space in memory using another small function:
<?php
/**
* Return whether a and b reference the same memory
*/
function reference(&$a, &$b) {
   
$d = refcount($b);
   
$e = &$a;
    return
refcount($b) != $d;
}

# Example
$a = 3;
$b = 5;
$c = &$a;
$d = &$c;

reference($a, $b) == false;
reference($a, $c) == true;
reference($a, $d) == true;
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites