2001-11-10 21:49:43 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<chapter xml:id="language.references" xmlns="http://docbook.org/ns/docbook">
|
2000-08-20 15:02:32 +00:00
|
|
|
<title>References Explained</title>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.references.whatare">
|
2000-09-06 21:27:43 +00:00
|
|
|
<title>What References Are</title>
|
2009-09-10 06:38:08 +00:00
|
|
|
<simpara>
|
|
|
|
References in PHP are a means to access the same variable content
|
|
|
|
by different names. They are not like C pointers; for instance,
|
|
|
|
you cannot perform pointer arithmetic using them, they are not
|
|
|
|
actual memory addresses, and so on. See
|
|
|
|
<xref linkend="language.references.arent" /> for more
|
|
|
|
information. Instead, 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 closest analogy is with
|
|
|
|
Unix filenames and files - variable names are directory entries,
|
|
|
|
while variable content is the file itself. References can be
|
|
|
|
likened to hardlinking in Unix filesystem.
|
2000-08-20 15:02:32 +00:00
|
|
|
</simpara>
|
|
|
|
</sect1>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml: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
|
2004-08-06 09:00:04 +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>
|
2005-04-15 14:19:52 +00:00
|
|
|
point to the same content.
|
2000-08-20 15:02:32 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
<varname>$a</varname> and <varname>$b</varname> are completely
|
2008-12-29 02:48:17 +00:00
|
|
|
equal here. <varname>$a</varname> is not pointing to
|
|
|
|
<varname>$b</varname> or vice versa.
|
|
|
|
<varname>$a</varname> and <varname>$b</varname> are pointing to the
|
2000-08-20 15:02:32 +00:00
|
|
|
same place.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</para>
|
2004-07-26 15:16:26 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
If arrays with references are copied, their values are not dereferenced.
|
2004-07-26 15:16:26 +00:00
|
|
|
This is valid also for arrays passed by value to functions.
|
|
|
|
</para>
|
|
|
|
</note>
|
2007-02-24 11:19:24 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
If you assign, pass, or return an undefined variable by reference,
|
2007-02-24 11:19:24 +00:00
|
|
|
it will get created.
|
|
|
|
<example>
|
|
|
|
<title>Using references with undefined variables</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
function foo(&$var) { }
|
|
|
|
|
|
|
|
foo($a); // $a is "created" and assigned to null
|
|
|
|
|
|
|
|
$b = array();
|
|
|
|
foo($b['b']);
|
|
|
|
var_dump(array_key_exists('b', $b)); // bool(true)
|
|
|
|
|
|
|
|
$c = new StdClass;
|
|
|
|
foo($c->d);
|
|
|
|
var_dump(property_exists($c, 'd')); // bool(true)
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</note>
|
2000-12-10 09:46:15 +00:00
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
The same syntax can be used with functions that return references,
|
|
|
|
and with the <literal>new</literal> operator (in PHP 4.0.4 and later):
|
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
|
|
|
$bar =& new fooclass();
|
2004-08-06 09:00:04 +00:00
|
|
|
$foo =& find_var($bar);
|
2003-06-09 19:28:28 +00:00
|
|
|
?>
|
2001-11-23 21:51:24 +00:00
|
|
|
]]>
|
2005-11-18 16:20:48 +00:00
|
|
|
</programlisting>
|
2000-12-10 09:46:15 +00:00
|
|
|
</informalexample>
|
2008-12-29 02:48:17 +00:00
|
|
|
Since PHP 5, <link linkend="language.oop5.basic.new">new</link> returns
|
|
|
|
a reference automatically, so using <literal>=&</literal> in this
|
2009-05-03 10:58:35 +00:00
|
|
|
context is deprecated and produces an <constant>E_STRICT</constant> message.
|
2005-11-18 16:20:48 +00:00
|
|
|
</para>
|
2000-12-10 09:46:15 +00:00
|
|
|
<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
|
2008-12-29 02:48:17 +00:00
|
|
|
Engine and will therefore result in a parse error.
|
2002-06-10 10:51:58 +00:00
|
|
|
</para>
|
2000-12-10 09:46:15 +00:00
|
|
|
</note>
|
2004-07-26 15:55:09 +00:00
|
|
|
<warning>
|
|
|
|
<para>
|
2004-08-08 17:26:59 +00:00
|
|
|
If you assign a reference to a variable declared <literal>global</literal>
|
|
|
|
inside a function, the reference will be visible only inside the function.
|
|
|
|
You can avoid this by using the <varname>$GLOBALS</varname> array.
|
2004-07-26 15:55:09 +00:00
|
|
|
<example>
|
2008-12-29 02:48:17 +00:00
|
|
|
<title>Referencing global variables inside functions</title>
|
2004-07-26 15:55:09 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
$var1 = "Example variable";
|
|
|
|
$var2 = "";
|
|
|
|
|
|
|
|
function global_references($use_globals)
|
|
|
|
{
|
|
|
|
global $var1, $var2;
|
|
|
|
if (!$use_globals) {
|
2004-08-05 15:05:59 +00:00
|
|
|
$var2 =& $var1; // visible only inside the function
|
2004-07-26 15:55:09 +00:00
|
|
|
} else {
|
2004-08-05 15:05:59 +00:00
|
|
|
$GLOBALS["var2"] =& $var1; // visible also in global context
|
2004-07-26 15:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global_references(false);
|
|
|
|
echo "var2 is set to '$var2'\n"; // var2 is set to ''
|
|
|
|
global_references(true);
|
|
|
|
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2004-07-26 20:38:58 +00:00
|
|
|
Think about <literal>global $var;</literal> as a shortcut to <literal>$var
|
2008-12-29 02:48:17 +00:00
|
|
|
=& $GLOBALS['var'];</literal>. Thus assigning another reference
|
2004-07-26 20:38:58 +00:00
|
|
|
to <literal>$var</literal> only changes the local variable's reference.
|
2004-07-26 15:55:09 +00:00
|
|
|
</para>
|
|
|
|
</warning>
|
2004-08-07 10:06:24 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2009-04-16 19:46:35 +00:00
|
|
|
If you assign a value to a variable with references in a
|
|
|
|
&foreach; statement, the references are modified too.
|
2004-08-07 10:06:24 +00:00
|
|
|
<example>
|
|
|
|
<title>References and foreach statement</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
$ref = 0;
|
|
|
|
$row =& $ref;
|
|
|
|
foreach (array(1, 2, 3) as $row) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
echo $ref; // 3 - last element of the iterated array
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</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
|
2008-12-29 02:48:17 +00:00
|
|
|
by reference. This is done by making a local variable in a function and
|
|
|
|
a variable in the calling scope referencing 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
|
2004-08-06 09:00:04 +00:00
|
|
|
function foo(&$var)
|
2001-08-19 17:03:32 +00:00
|
|
|
{
|
2000-08-20 15:02:32 +00:00
|
|
|
$var++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a=5;
|
2004-08-06 09:00:04 +00:00
|
|
|
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
|
2008-12-29 02:48:17 +00:00
|
|
|
<varname>$a</varname>. For more information on this, read the <link
|
|
|
|
linkend="language.references.pass">passing by reference</link> section.
|
2000-08-19 09:12:18 +00:00
|
|
|
</para>
|
2000-08-20 15:02:32 +00:00
|
|
|
<simpara>
|
2008-12-29 02:48:17 +00:00
|
|
|
The third thing references 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>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml: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>
|
2008-12-29 02:48:17 +00:00
|
|
|
As said before, references are not 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
|
2004-08-06 09:00:04 +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
|
2008-12-29 02:48:17 +00:00
|
|
|
<varname>$bar</varname> in the caller, but then
|
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).
|
2004-08-06 21:39:39 +00:00
|
|
|
You can use <link linkend="language.references.return">returning
|
2004-08-06 21:45:13 +00:00
|
|
|
references</link> to reference variables selected by the function.
|
2000-08-20 15:02:32 +00:00
|
|
|
</simpara>
|
|
|
|
</sect1>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.references.pass">
|
2000-12-10 09:46:15 +00:00
|
|
|
<title>Passing by Reference</title>
|
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
You can pass a variable by reference to a function so the function
|
|
|
|
can modify the variable. 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
|
2004-08-06 09:00:04 +00:00
|
|
|
function foo(&$var)
|
2001-08-19 17:03:32 +00:00
|
|
|
{
|
2000-12-10 09:46:15 +00:00
|
|
|
$var++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a=5;
|
2004-08-06 09:00:04 +00:00
|
|
|
foo($a);
|
2000-12-10 09:46:15 +00:00
|
|
|
// $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>
|
2009-08-13 03:32:47 +00:00
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
There is no reference sign on a function call - only on
|
|
|
|
function definitions. Function definitions alone are enough to
|
|
|
|
correctly pass the argument by reference. As of PHP 5.3.0,
|
|
|
|
you will get a warning saying that "call-time pass-by-reference" is
|
|
|
|
deprecated when you use & in <literal>foo(&$a);</literal>.
|
|
|
|
</simpara>
|
|
|
|
</note>
|
2000-12-10 09:46:15 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2005-07-06 14:26:29 +00:00
|
|
|
The following things can be passed by reference:
|
2000-12-10 09:46:15 +00:00
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2008-12-29 02:48:17 +00:00
|
|
|
Variables, i.e. <literal>foo($a)</literal>
|
2000-12-10 09:46:15 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2008-12-29 02:48:17 +00:00
|
|
|
New statements, i.e. <literal>foo(new foobar())</literal>
|
2000-12-10 09:46:15 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
References returned from functions, i.e.:
|
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 &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>
|
2008-12-29 02:48:17 +00:00
|
|
|
See more about <link
|
2000-12-10 09:46:15 +00:00
|
|
|
linkend="language.references.return">returning by reference</link>.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
No other expressions should be passed by reference, as the
|
2000-12-10 09:46:15 +00:00
|
|
|
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
|
|
|
}
|
2005-09-16 23:01:50 +00:00
|
|
|
foo(bar()); // Produces fatal error since PHP 5.0.5
|
2000-12-10 09:46:15 +00:00
|
|
|
|
2003-12-11 15:42:10 +00:00
|
|
|
foo($a = 5); // Expression, not variable
|
2005-07-14 09:16:30 +00:00
|
|
|
foo(5); // Produces fatal error
|
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.
|
2009-08-13 03:32:47 +00:00
|
|
|
</para>
|
2000-12-10 09:46:15 +00:00
|
|
|
</sect1>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.references.return">
|
2000-08-20 15:02:32 +00:00
|
|
|
<title>Returning References</title>
|
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
Returning by reference is useful when you want to use a function
|
|
|
|
to find to which variable a reference should be bound. Do
|
|
|
|
<emphasis>not</emphasis> use return-by-reference to increase performance.
|
|
|
|
The engine will automatically optimize this on its own. Only return
|
|
|
|
references when you have a valid technical reason to do so. To
|
2005-07-06 14:26:29 +00:00
|
|
|
return 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
|
2007-11-12 22:54:49 +00:00
|
|
|
class foo {
|
|
|
|
public $value = 42;
|
|
|
|
|
|
|
|
public function &getValue() {
|
|
|
|
return $this->value;
|
|
|
|
}
|
2000-08-20 15:02:32 +00:00
|
|
|
}
|
|
|
|
|
2007-11-12 22:54:49 +00:00
|
|
|
$obj = new foo;
|
|
|
|
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
|
|
|
|
$obj->value = 2;
|
|
|
|
echo $myValue; // prints the new value of $obj->value, i.e. 2.
|
|
|
|
?>
|
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
|
2007-11-12 22:54:49 +00:00
|
|
|
<varname>getValue</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
|
2008-12-29 02:48:17 +00:00
|
|
|
want to return by reference, not a copy, and to indicate that
|
2000-10-04 02:02:28 +00:00
|
|
|
reference binding, rather than usual assignment, should be done
|
2007-11-12 22:54:49 +00:00
|
|
|
for <varname>$myValue</varname>.
|
2005-07-06 12:52:04 +00:00
|
|
|
</simpara>
|
|
|
|
</note>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
If you try to return a reference from a function with the syntax:
|
2007-11-12 22:54:49 +00:00
|
|
|
<literal>return ($this->value);</literal> this will <emphasis>not</emphasis>
|
2005-07-06 14:26:29 +00:00
|
|
|
work as you are attempting to return the result of an
|
2005-07-06 12:52:04 +00:00
|
|
|
<emphasis>expression</emphasis>, and not a variable, by reference. You can
|
|
|
|
only return variables by reference from a function - nothing else.
|
2008-12-29 02:48:17 +00:00
|
|
|
Since PHP 4.4.0 in the PHP4 branch, and PHP 5.1.0 in the PHP5 branch, an
|
|
|
|
<constant>E_NOTICE</constant> error is issued if the code tries to return
|
|
|
|
a dynamic expression or a result of the <literal>new</literal> operator.
|
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
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.references.unset">
|
2000-08-20 15:02:32 +00:00
|
|
|
<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;
|
2004-08-06 09:00:04 +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>
|
2008-12-29 02:48:17 +00:00
|
|
|
Again, it might be useful to think about this as analogous to the Unix
|
2000-08-20 15:02:32 +00:00
|
|
|
<command>unlink</command> call.
|
|
|
|
</simpara>
|
|
|
|
</sect1>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml: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
|
2008-12-29 02:48:17 +00:00
|
|
|
mechanisms, so everything mentioned herein about reference binding also
|
|
|
|
applies to these constructs. Some constructs, like passing and
|
|
|
|
returning by reference, are mentioned above. Other constructs that
|
2000-08-20 15:02:32 +00:00
|
|
|
use references are:
|
|
|
|
</simpara>
|
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect2 xml:id="references.global">
|
2009-05-03 10:58:35 +00:00
|
|
|
<title>global References</title>
|
2000-08-20 15:02:32 +00:00
|
|
|
<para>
|
2008-12-29 02:48:17 +00:00
|
|
|
When you declare a variable as <command>global $var</command> you
|
2000-08-20 15:02:32 +00:00
|
|
|
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>
|
2008-12-29 02:48:17 +00:00
|
|
|
This also means that unsetting <varname>$var</varname>
|
|
|
|
won't unset the 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
|
|
|
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect2 xml:id="references.this">
|
2000-08-20 15:02:32 +00:00
|
|
|
<title><literal>$this</literal></title>
|
|
|
|
<simpara>
|
2005-07-06 12:52:04 +00:00
|
|
|
In an object method, <varname>$this</varname> is always a reference
|
2000-08-20 15:02:32 +00:00
|
|
|
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
|
|
|
-->
|