1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<refentry id="function.debug-zval-dump">
<refnamediv>
<refname>debug_zval_dump</refname>
<refpurpose>Dumps a string representation of an internal zend value to output</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>void</type><methodname>debug_zval_dump</methodname>
<methodparam><type>mixed</type><parameter>variable</parameter></methodparam>
</methodsynopsis>
<para>
Dumps a string representation of an internal zend value to output.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>variable</parameter></term>
<listitem>
<para>
The variable being evaluated.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>debug_zval_dump</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump(&$var1);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(11) "Hello World" refcount(3)
]]>
</screen>
</example>
</para>
<note>
<title>Beware the <literal>refcount</literal></title>
<para>
The <literal>refcount</literal> value returned by this function is
non-obvious in certain circumstances. For example, a developer might
expect the above example to indicate a <literal>refcount</literal> of
<literal>2</literal>. The third reference is created when actually
calling <function>debug_zval_dump</function>.
</para>
<para>
This behavior is further compounded when a variable is not passed to
<function>debug_zval_dump</function> by reference. To illustrate, consider
a slightly modified version of the above example:
</para>
<para>
<example>
<title/>
<programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello World';
$var2 = '';
$var2 =& $var1;
debug_zval_dump($var1); // not passed by reference, this time
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(11) "Hello World" refcount(1)
]]>
</screen>
</example>
</para>
<para>
Why <literal>refcount(1)</literal>? Because a copy of <literal>$var1</literal> is
being made, when the function is called.
</para>
<para>
This function becomes even <emphasis>more</emphasis> confusing when a
variable with a <literal>refcount</literal> of <literal>1</literal> is
passed (by copy/value):
</para>
<para>
<example>
<title/>
<programlisting role="php">
<![CDATA[
<?php
$var1 = 'Hello World';
debug_zval_dump($var1);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(11) "Hello World" refcount(2)
]]>
</screen>
</example>
</para>
<para>
A <literal>refcount</literal> of <literal>2</literal>, here, is extremely
non-obvious. Especially considering the above examples. So what's
happening?
</para>
<para>
When a variable has a single reference (as did <literal>$var1</literal>
before it was used as an argument to <function>debug_zval_dump</function>),
PHP's engine optimizes the manner in which it is passed to a function.
Internally, PHP treats <literal>$var1</literal> like a reference (in that
the <literal>refcount</literal> is increased for the scope of this
function), with the caveat that <emphasis>if</emphasis> the passed reference
happens to be written to, a copy is made, but only at the moment of
writing. This is known as "copy on write."
</para>
<para>
So, if <function>debug_zval_dump</function> happened to write to its sole
parameter (and it doesn't), then a copy would be made. Until then, the
parameter remains a reference, causing the <literal>refcount</literal> to
be incremented to <literal>2</literal> for the scope of the function call.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>var_dump</function></member>
<member><function>debug_backtrace</function></member>
<member><link linkend="language.references">References Explained</link></member>
<member><ulink url="&url.zend.references;">Reference Counting and Aliasing (by Andi Gutmans)</ulink></member>
<member><ulink url="&url.derick.references;">References Explained (by Derick Rethans)</ulink></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|