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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
<?xml version="1.0" encoding="big5"?>
<chapter id="language.references">
<title>References Explained</title>
<sect1 id="language.references.whatare">
<title>What References Are</title>
<simpara>
References are a means in PHP to access the same variable content
by different names. They are not like C pointers, they are symbol
table aliases. Note that in PHP, variable name and variable content
are different, so the same content can have different names.
The most close analogy is with Unix filenames and files -
variable names are directory entries, while variable contents is
the file itself. References can be thought of as hardlinking in
Unix filesystem.
</simpara>
</sect1>
<sect1 id="language.references.whatdo">
<title>What References Do</title>
<para>
PHP references allow you to make two variables to refer to the
same content. Meaning, when you do:
<informalexample>
<programlisting role="php">
$a =& $b
</programlisting>
</informalexample>
it means that <varname>$a</varname> and <varname>$b</varname>
point to the same variable.
<note>
<para>
<varname>$a</varname> and <varname>$b</varname> are completely
equal here, that's not <varname>$a</varname> is pointing to
<varname>$b</varname> or vice versa, that's
<varname>$a</varname> and <varname>$b</varname> pointing to the
same place.
</para>
</note>
</para>
<para>
The same syntax can be used with functions, that return references,
and with <literal>new</literal> operator (in PHP 4.0.4 and later):
<informalexample>
<programlisting role="php">
$bar =& new fooclass();
$foo =& find_var ($bar);
</programlisting>
</informalexample>
</para>
<note>
<para>
Unless you use the syntax above, the result of
<literal>$bar = new fooclass()</literal> will not be the same
variable as <literal>$this</literal> in the constructor, meaning
that if you have used reference to <literal>$this</literal> in
the constructor, you should use reference assignment, or you get
two different objects.
</para>
</note>
<para>
The second thing references do is to pass variables
by-reference. This is done by making a local variable in a function and
a variable in the calling scope reference to the same content. Example:
<informalexample>
<programlisting role="php">
function foo (&$var) {
$var++;
}
$a=5;
foo ($a);
</programlisting>
</informalexample>
will make <varname>$a</varname> to be 6. This happens because in
the function <varname>foo</varname> the variable
<varname>$var</varname> refers to the same content as
<varname>$a</varname>. See also more detailed explanations about <link
linkend="language.references.pass">passing by reference</link>.
</para>
<simpara>
The third thing reference can do is <link
linkend="language.references.return">return by reference</link>.
</simpara>
</sect1>
<sect1 id="language.references.arent">
<title>What References Are Not</title>
<para>
As said before, references aren't pointers. That means, the
following construct won't do what you expect:
<informalexample>
<programlisting role="php">
function foo (&$var) {
$var =& $GLOBALS["baz"];
}
foo($bar);
</programlisting>
</informalexample>
</para>
<simpara>
What happens is that <varname>$var</varname> in foo will be bound
with <varname>$bar</varname> in caller, but then it will be
re-bound with <varname>$GLOBALS["baz"]</varname>. There's no way
to bind <varname>$bar</varname> in the calling scope to something else
using the reference mechanism, since <varname>$bar</varname> is not
available in the function foo (it is represented by
<varname>$var</varname>, but <varname>$var</varname> has only
variable contents and not name-to-value binding in the calling
symbol table).
</simpara>
</sect1>
<sect1 id="language.references.pass">
<title>Passing by Reference</title>
<para>
You can pass variable to function by reference, so that function could modify
its arguments. The sytax is as follows:
<informalexample>
<programlisting role="php">
function foo (&$var) {
$var++;
}
$a=5;
foo ($a);
// $a is 6 here
</programlisting>
</informalexample>
Note that there's no reference sign on function call - only on
function definition. Function definition alone is enough to
correctly pass the argument by reference.
</para>
<para>
Following things can be passed by reference:
<itemizedlist>
<listitem>
<simpara>
Variable, i.e. <literal>foo($a)</literal>
</simpara>
</listitem>
<listitem>
<simpara>
New statement, i.e. <literal>foo(new foobar())</literal>
</simpara>
</listitem>
<listitem>
<para>
Reference, returned from a function, i.e.:
<informalexample>
<programlisting role="php">
function &bar()
{
$a = 5;
return $a;
}
foo(bar());
</programlisting>
</informalexample>
See also explanations about <link
linkend="language.references.return">returning by reference</link>.
</para>
</listitem>
</itemizedlist>
</para>
<para>
Any other expression should not be passed by reference, as the
result is undefined. For example, the following examples of passing
by reference are invalid:
<informalexample>
<programlisting role="php">
function bar() // Note the missing &
{
$a = 5;
return $a;
}
foo(bar));
foo($a = 5) // Expression, not variable
foo(5) // Constant, not variable
</programlisting>
</informalexample>
These requirements are for PHP 4.0.4 and later.
</para>
</sect1>
<sect1 id="language.references.return">
<title>Returning References</title>
<para>
Returning by-reference is useful when you want to use a function
to find which variable a reference should be bound to. When
returning references, use this syntax:
<informalexample>
<programlisting role="php">
function &find_var ($param) {
...code...
return $found_var;
}
$foo =& find_var ($bar);
$foo->x = 2;
</programlisting>
</informalexample>
In this example, the property of the object returned by the
<varname>find_var</varname> function would be set, not the
copy, as it would be without using reference syntax.
</para>
<note>
<simpara>
Unlike parameter passing, here you have to use
<literal>&</literal> in both places - to indicate that you
return by-reference, not a copy as usual, and to indicate that
reference binding, rather than usual assignment, should be done
for <varname>$foo</varname>.
</simpara>
</note>
</sect1>
<sect1 id="language.references.unset">
<title>Unsetting References</title>
<para>
When you unset the reference, you just break the binding between
variable name and variable content. This does not mean that
variable content will be destroyed. For example:
<informalexample>
<programlisting role="php">
$a = 1;
$b =& $a;
unset ($a);
</programlisting>
</informalexample>
won't unset <varname>$b</varname>, just <varname>$a</varname>.
</para>
<simpara>
Again, it might be useful to think about this as analogous to Unix
<command>unlink</command> call.
</simpara>
</sect1>
<sect1 id="language.references.spot">
<title>Spotting References</title>
<simpara>
Many syntax constructs in PHP are implemented via referencing
mechanisms, so everything told above about reference binding also
apply to these constructs. Some constructs, like passing and
returning by-reference, are mentioned above. Other constructs that
use references are:
</simpara>
<sect2 id="references.global">
<title><literal>global</literal> References</title>
<para>
When you declare variable as <command>global $var</command> you
are in fact creating reference to a global variable. That means,
this is the same as:
<informalexample>
<programlisting role="php">
$var =& $GLOBALS["var"];
</programlisting>
</informalexample>
</para>
<simpara>
That means, for example, that unsetting <varname>$var</varname>
won't unset global variable.
</simpara>
</sect2>
<sect2 id="references.this">
<title><literal>$this</literal></title>
<simpara>
In an object method, <varname>$this</varname> is always reference
to the caller object.
</simpara>
</sect2>
</sect1>
</chapter>
<!-- 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:
-->
|