mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Add more explanations, small fixes
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@30582 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
987c42587c
commit
4a2d9feae9
1 changed files with 43 additions and 35 deletions
|
@ -21,8 +21,7 @@
|
|||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
$a =& $b
|
||||
</programlisting>
|
||||
$a =& $b </programlisting>
|
||||
</informalexample>
|
||||
|
||||
it means that <varname>$a</varname> and <varname>$b</varname> point to
|
||||
|
@ -38,23 +37,27 @@ $a =& $b
|
|||
</para>
|
||||
|
||||
<para>
|
||||
The second thing references do is to pass variables by-references. This is
|
||||
The second thing references do is to pass variables by-reference. This is
|
||||
done by making local function variable and caller variable to be reference
|
||||
to the same content. Example:
|
||||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
function foo(&$var) {
|
||||
$var++;
|
||||
}
|
||||
function foo(&$var) {
|
||||
$var++;
|
||||
}
|
||||
|
||||
$a=5;
|
||||
foo($a);
|
||||
</programlisting>
|
||||
$a=5;
|
||||
foo($a);</programlisting>
|
||||
</informalexample>
|
||||
|
||||
will make <varname>$a</varname> to be 6.
|
||||
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>.
|
||||
</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">
|
||||
|
@ -65,22 +68,22 @@ foo($a);
|
|||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
function foo(&$var) {
|
||||
$var =& $GLOBALS["baz"];
|
||||
}
|
||||
foo($bar);
|
||||
</programlisting>
|
||||
function foo(&$var) {
|
||||
$var =& $GLOBALS["baz"];
|
||||
}
|
||||
foo($bar); </programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
|
||||
<simpara>
|
||||
What will happen 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 caller to something else using 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).
|
||||
<varname>$GLOBALS["baz"]</varname>. There's no way to bind
|
||||
<varname>$bar</varname> in the caller to something else using 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>
|
||||
|
||||
|
@ -93,18 +96,25 @@ foo($bar);
|
|||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
function &find_var($param) {
|
||||
...code...
|
||||
return $found_var;
|
||||
}
|
||||
function &find_var($param) {
|
||||
...code...
|
||||
return $found_var;
|
||||
}
|
||||
|
||||
$foo =& find_var($bar);
|
||||
</programlisting>
|
||||
$foo =& find_var($bar);
|
||||
$foo->x = 2; </programlisting>
|
||||
</informalexample>
|
||||
|
||||
In this example, property of the object returned by the
|
||||
<varname>find_var</varname> function would be set, not of the copy, as
|
||||
it would be without using reference syntax.
|
||||
</para>
|
||||
<note>
|
||||
<simpara>
|
||||
Unlike parameter passing, here you use & in both places.
|
||||
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 than reference binding and not usual
|
||||
assignment should be done for <varname>$foo</varname>.
|
||||
</simpara>
|
||||
</note>
|
||||
</sect1>
|
||||
|
@ -118,19 +128,17 @@ $foo =& find_var($bar);
|
|||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
$a = 1;
|
||||
$b =& $a;
|
||||
unset($a);
|
||||
</programlisting>
|
||||
$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 unlink
|
||||
call.
|
||||
Again, it might be useful to think about this as analogous to Unix
|
||||
<command>unlink</command> call.
|
||||
</simpara>
|
||||
</sect1>
|
||||
|
||||
|
@ -154,7 +162,7 @@ unset($a);
|
|||
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
$var =& $GLOBALS["var"];</programlisting>
|
||||
$var =& $GLOBALS["var"];</programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
|
||||
|
|
Loading…
Reference in a new issue