2001-11-10 21:49:43 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2004-07-26 15:16:26 +00:00
|
|
|
<!-- $Revision: 1.28 $ -->
|
2000-08-20 15:02:32 +00:00
|
|
|
<chapter id="language.references">
|
|
|
|
<title>References Explained</title>
|
|
|
|
|
|
|
|
<sect1 id="language.references.whatare">
|
2000-09-06 21:27:43 +00:00
|
|
|
<title>What References Are</title>
|
2000-08-20 15:02:32 +00:00
|
|
|
<simpara>
|
2002-06-30 10:41:44 +00:00
|
|
|
References in PHP are a means to access the same variable content
|
2000-12-20 07:19:44 +00:00
|
|
|
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 -
|
2000-08-20 15:02:32 +00:00
|
|
|
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">
|
2000-09-06 21:27:43 +00:00
|
|
|
<title>What References Do</title>
|
2000-08-20 15:02:32 +00:00
|
|
|
<para>
|
|
|
|
PHP references allow you to make two variables to refer to the
|
|
|
|
same content. Meaning, when you do:
|
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
$a =& $b
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-08-20 15:02:32 +00:00
|
|
|
</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>
|
2004-07-26 15:16:26 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
If array with references is copied, its values are not dereferenced.
|
|
|
|
This is valid also for arrays passed by value to functions.
|
|
|
|
</para>
|
|
|
|
</note>
|
2000-12-10 09:46:15 +00:00
|
|
|
<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">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
$bar =& new fooclass();
|
|
|
|
$foo =& find_var ($bar);
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-12-10 09:46:15 +00:00
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<para>
|
2001-12-12 21:29:56 +00:00
|
|
|
Not using the <literal>&</literal> operator causes a copy of the
|
|
|
|
object to be made. If you use <literal>$this</literal> in the class it
|
|
|
|
will operate on the current instance of the class. The assignment without
|
|
|
|
<literal>&</literal> will copy the instance (i.e. the object) and
|
|
|
|
<literal>$this</literal> will operate on the copy, which is not always
|
|
|
|
what is desired. Usually you want to have a single instance to work with,
|
|
|
|
due to performance and memory consumption issues.
|
2000-12-10 09:46:15 +00:00
|
|
|
</para>
|
2002-06-10 10:51:58 +00:00
|
|
|
<para>
|
|
|
|
While you can use the <literal>@</literal> operator to
|
|
|
|
<emphasis>mute</emphasis> any errors in the constructor when using it as
|
|
|
|
<literal>@new</literal>, this does not work when using the
|
|
|
|
<literal>&new</literal> statement. This is a limitation of the Zend
|
|
|
|
Engine and will therefore result in a parser error.
|
|
|
|
</para>
|
2000-12-10 09:46:15 +00:00
|
|
|
</note>
|
2000-08-19 09:12:18 +00:00
|
|
|
<para>
|
2000-08-20 15:02:32 +00:00
|
|
|
The second thing references do is to pass variables
|
2000-12-20 07:19:44 +00:00
|
|
|
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:
|
2000-08-20 15:02:32 +00:00
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
function foo (&$var)
|
2001-08-19 17:03:32 +00:00
|
|
|
{
|
2000-08-20 15:02:32 +00:00
|
|
|
$var++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a=5;
|
|
|
|
foo ($a);
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-08-20 15:02:32 +00:00
|
|
|
</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
|
2000-12-10 09:46:15 +00:00
|
|
|
<varname>$a</varname>. See also more detailed explanations about <link
|
|
|
|
linkend="language.references.pass">passing by reference</link>.
|
2000-08-19 09:12:18 +00:00
|
|
|
</para>
|
2000-08-20 15:02:32 +00:00
|
|
|
<simpara>
|
|
|
|
The third thing reference can do is <link
|
2000-12-10 09:46:15 +00:00
|
|
|
linkend="language.references.return">return by reference</link>.
|
2000-08-20 15:02:32 +00:00
|
|
|
</simpara>
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="language.references.arent">
|
2000-09-06 21:27:43 +00:00
|
|
|
<title>What References Are Not</title>
|
2000-08-20 15:02:32 +00:00
|
|
|
<para>
|
2000-12-20 07:19:44 +00:00
|
|
|
As said before, references aren't pointers. That means, the
|
2000-08-20 15:02:32 +00:00
|
|
|
following construct won't do what you expect:
|
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-12-03 18:12:53 +00:00
|
|
|
function foo (&$var)
|
2001-08-19 17:03:32 +00:00
|
|
|
{
|
2001-11-23 21:51:24 +00:00
|
|
|
$var =& $GLOBALS["baz"];
|
2000-08-20 15:02:32 +00:00
|
|
|
}
|
|
|
|
foo($bar);
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-08-20 15:02:32 +00:00
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
|
|
|
</para>
|
|
|
|
<simpara>
|
2001-08-19 17:03:32 +00:00
|
|
|
What happens is that <varname>$var</varname> in
|
|
|
|
<varname>foo</varname> will be bound with
|
|
|
|
<varname>$bar</varname> in caller, but then it will be
|
2000-08-20 15:02:32 +00:00
|
|
|
re-bound with <varname>$GLOBALS["baz"]</varname>. There's no way
|
2000-12-20 07:19:44 +00:00
|
|
|
to bind <varname>$bar</varname> in the calling scope to something else
|
|
|
|
using the reference mechanism, since <varname>$bar</varname> is not
|
2001-08-19 17:03:32 +00:00
|
|
|
available in the function <varname>foo</varname> (it is represented by
|
2000-08-20 15:02:32 +00:00
|
|
|
<varname>$var</varname>, but <varname>$var</varname> has only
|
|
|
|
variable contents and not name-to-value binding in the calling
|
|
|
|
symbol table).
|
|
|
|
</simpara>
|
|
|
|
</sect1>
|
|
|
|
|
2000-12-10 09:46:15 +00:00
|
|
|
<sect1 id="language.references.pass">
|
|
|
|
<title>Passing by Reference</title>
|
|
|
|
<para>
|
|
|
|
You can pass variable to function by reference, so that function could modify
|
2001-07-11 08:41:49 +00:00
|
|
|
its arguments. The syntax is as follows:
|
2000-12-10 09:46:15 +00:00
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
function foo (&$var)
|
2001-08-19 17:03:32 +00:00
|
|
|
{
|
2000-12-10 09:46:15 +00:00
|
|
|
$var++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a=5;
|
|
|
|
foo ($a);
|
|
|
|
// $a is 6 here
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-12-10 09:46:15 +00:00
|
|
|
</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">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
function &bar()
|
2000-12-10 09:46:15 +00:00
|
|
|
{
|
2001-08-20 13:58:08 +00:00
|
|
|
$a = 5;
|
|
|
|
return $a;
|
2000-12-10 09:46:15 +00:00
|
|
|
}
|
|
|
|
foo(bar());
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-12-10 09:46:15 +00:00
|
|
|
</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">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
function bar() // Note the missing &
|
2000-12-10 09:46:15 +00:00
|
|
|
{
|
2001-08-20 13:58:08 +00:00
|
|
|
$a = 5;
|
|
|
|
return $a;
|
2000-12-10 09:46:15 +00:00
|
|
|
}
|
2001-08-19 17:03:32 +00:00
|
|
|
foo(bar());
|
2000-12-10 09:46:15 +00:00
|
|
|
|
2003-12-11 15:42:10 +00:00
|
|
|
foo($a = 5); // Expression, not variable
|
|
|
|
foo(5); // Constant, not variable
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-12-10 09:46:15 +00:00
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
2003-12-21 15:37:29 +00:00
|
|
|
These requirements are for PHP 4.0.4 and later.
|
2000-12-10 09:46:15 +00:00
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2000-08-20 15:02:32 +00:00
|
|
|
<sect1 id="language.references.return">
|
|
|
|
<title>Returning References</title>
|
|
|
|
<para>
|
2000-12-20 07:21:09 +00:00
|
|
|
Returning by-reference is useful when you want to use a function
|
2000-10-04 02:02:28 +00:00
|
|
|
to find which variable a reference should be bound to. When
|
|
|
|
returning references, use this syntax:
|
2000-08-20 15:02:32 +00:00
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
function &find_var ($param)
|
2001-08-19 17:03:32 +00:00
|
|
|
{
|
2003-12-11 15:42:10 +00:00
|
|
|
/* ...code... */
|
2000-08-20 15:02:32 +00:00
|
|
|
return $found_var;
|
|
|
|
}
|
|
|
|
|
2001-11-23 21:51:24 +00:00
|
|
|
$foo =& find_var ($bar);
|
2003-12-21 15:37:29 +00:00
|
|
|
$foo->x = 2;
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-08-20 15:02:32 +00:00
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
2000-10-04 02:02:28 +00:00
|
|
|
In this example, the property of the object returned by the
|
|
|
|
<varname>find_var</varname> function would be set, not the
|
2000-08-20 15:02:32 +00:00
|
|
|
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
|
2000-10-04 02:02:28 +00:00
|
|
|
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>.
|
2000-08-20 15:02:32 +00:00
|
|
|
</simpara>
|
2000-08-19 09:12:18 +00:00
|
|
|
</note>
|
2000-08-20 15:02:32 +00:00
|
|
|
</sect1>
|
2000-08-19 09:12:18 +00:00
|
|
|
|
2000-08-20 15:02:32 +00:00
|
|
|
<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">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2000-08-20 15:02:32 +00:00
|
|
|
$a = 1;
|
2001-12-03 18:12:53 +00:00
|
|
|
$b =& $a;
|
2000-08-20 15:02:32 +00:00
|
|
|
unset ($a);
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-08-20 15:02:32 +00:00
|
|
|
</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">
|
2000-09-06 21:27:43 +00:00
|
|
|
<title>Spotting References</title>
|
2000-08-20 15:02:32 +00:00
|
|
|
<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">
|
2001-11-23 21:51:24 +00:00
|
|
|
<![CDATA[
|
2003-06-09 19:28:28 +00:00
|
|
|
<?php
|
2001-11-23 21:51:24 +00:00
|
|
|
$var =& $GLOBALS["var"];
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2000-08-20 15:02:32 +00:00
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
|
|
|
</para>
|
2000-08-19 09:12:18 +00:00
|
|
|
<simpara>
|
2000-08-20 15:02:32 +00:00
|
|
|
That means, for example, that unsetting <varname>$var</varname>
|
|
|
|
won't unset global variable.
|
2000-08-19 09:12:18 +00:00
|
|
|
</simpara>
|
2000-08-20 15:02:32 +00:00
|
|
|
</sect2>
|
2000-08-19 09:12:18 +00:00
|
|
|
|
2000-08-20 15:02:32 +00:00
|
|
|
<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
|
2001-12-12 20:47:43 +00:00
|
|
|
indent-tabs-mode:nil
|
2000-08-20 15:02:32 +00:00
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../../manual.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
2001-09-21 22:47:49 +00:00
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
2000-08-20 15:02:32 +00:00
|
|
|
-->
|