svn: /phpdoc/en/trunk/language/ references.xml
| From: | Gustavo André dos Santos Lopes | Date: | Fri, 22 Oct 2010 11:26:50 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/ references.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-7288@lists.php.net to get a copy of this message | ||
cataphract Fri, 22 Oct 2010 11:26:50 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=304613
Log:
- Added information about references in expressions created
with array().
- Added parenthesis to "new returns a reference" statement,
with a link to language.oop5.references.
Changed paths:
U phpdoc/en/trunk/language/references.xml
Modified: phpdoc/en/trunk/language/references.xml
===================================================================
--- phpdoc/en/trunk/language/references.xml 2010-10-22 11:05:22 UTC (rev 304612)
+++ phpdoc/en/trunk/language/references.xml 2010-10-22 11:26:50 UTC (rev 304613)
@@ -2,7 +2,7 @@
<!-- $Revision$ -->
<chapter xml:id="language.references" xmlns="http://docbook.org/ns/docbook">
<title>References Explained</title>
-
+
<sect1 xml:id="language.references.whatare">
<title>What References Are</title>
<simpara>
@@ -107,6 +107,11 @@
using <literal>=&</literal> in this context is deprecated and
produces an <constant>E_DEPRECATED</constant> message in PHP 5.3 and
later, and an <constant>E_STRICT</constant> message in earlier versions.
+ (Technically, the difference is that in PHP 5 object variables, much like
+ resources, are a mere pointer to the actual object data, so these object
+ references are not "references" in the same sense used before (aliases).
+ For more information, see <link linkend="language.oop5.references">Objects
+ and references</link>.)
</para>
<warning>
<para>
@@ -165,6 +170,56 @@
</example>
</para>
</note>
+ <para>
+ While not being strictly an assignment by reference, expressions created
+ with the language construct
+ <link
linkend="function.array"><literal>array()</literal></link> can also
+ behave as such by prefixing <literal>&</literal> to the array element
+ to add. Example:
+ <informalexample>
+ <programlisting role="php">
+<![CDATA[
+<?php
+$a = 1;
+$b = array(2, 3);
+$arr = array(&$a, &$b[0], &$b[1]);
+$arr[0]++; $arr[1]++; $arr[2]++;
+/* $a == 2, $b == array(3, 4); */
+?>
+]]>
+ </programlisting>
+ </informalexample>
+ </para>
+ <para>
+ Note, however, that references inside arrays are potentially dangerous.
+ While doing a normal (not by reference) assignment with a reference on the
+ right side does not turn the left side into a reference, references inside
+ arrays are preserved in normal assignments. Example:
+ <informalexample>
+ <programlisting role="php">
+<![CDATA[
+<?php
+/* Assignment of scalar variables */
+$a = 1;
+$b =& $a;
+$c = $b;
+$c = 7; //$c is not a reference; no change to $a or $b
+
+/* Assignment of array variables */
+$a = 1;
+$arr = array(&$a);
+$arr2 = $arr; //not an assignment-by-reference!
+$arr2[0]++;
+/* $a == 2, $arr == array(2) */
+/* The contents of $arr are changed even though it's not a reference! */
+?>
+]]>
+ </programlisting>
+ </informalexample>
+ In other words, the reference behavior of arrays is defined in an
+ element-by-element basis; the reference behavior of individual elements
+ is dissociated from the reference status of the array container.
+ </para>
</sect2>
<sect2 xml:id="language.references.whatdo.pass">
<title>Pass By Reference</title>