svn: /phpdoc/en/trunk/language/ operators.xml references.xml
| From: | Adam Harvey | Date: | Fri, 22 Oct 2010 08:15:45 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/ operators.xml references.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-7287@lists.php.net to get a copy of this message | ||
aharvey Fri, 22 Oct 2010 08:15:45 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=304605
Log:
Fix doc bug #53117 (Assignment by reference within assignment operator) by
breaking out the previously brief discussion of using the assignment operator
to assign by reference into its own subsection, including a reference to
by-reference assignments of new expressions being deprecated. Also updated the
References Explained section to reflect 5.3+ generating E_DEPRECATED errors in
that case, rather than E_STRICT.
Bug: http://bugs.php.net/53117 (Assigned) Assignment by
reference within assignment operator
Changed paths:
U phpdoc/en/trunk/language/operators.xml
U phpdoc/en/trunk/language/references.xml
Modified: phpdoc/en/trunk/language/operators.xml
===================================================================
--- phpdoc/en/trunk/language/operators.xml 2010-10-22 07:41:25 UTC (rev 304604)
+++ phpdoc/en/trunk/language/operators.xml 2010-10-22 08:15:45 UTC (rev 304605)
@@ -364,17 +364,75 @@
Note that the assignment copies the original variable to the new
one (assignment by value), so changes to one will not affect the
other. This may also have relevance if you need to copy something
- like a large array inside a tight loop. Assignment
- by reference is also supported, using the <computeroutput>$var =
- &$othervar;</computeroutput> syntax.
- 'Assignment by reference' means that both variables end
- up pointing at the same data, and nothing is copied anywhere.
- To learn more about references, please read <link
- linkend="language.references">References explained</link>. As of
- PHP 5, objects are assigned by reference unless explicitly told
- otherwise with the new <link linkend="language.oop5.cloning">clone</link>
- keyword.
+ like a large array inside a tight loop.
</para>
+ <para>
+ An exception to the usual assignment by value behaviour within PHP occurs
+ with <type>object</type>s, which are assigned by reference in PHP 5.
+ Objects may be explicitly copied via the <link
+ linkend="language.oop5.cloning">clone</link> keyword.
+ </para>
+
+ <sect2 xml:id="language.operators.assignment.reference">
+ <title>Assignment by Reference</title>
+ <para>
+ Assignment by reference is also supported, using the
+ "<computeroutput>$var = &$othervar;</computeroutput>" syntax.
+ Assignment by reference means that both variables end up pointing at the
+ same data, and nothing is copied anywhere.
+ </para>
+ <para>
+ <example>
+ <title>Assigning by reference</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+$a = 3;
+$b = &$a; // $b is a reference to $a
+
+print "$a\n"; // prints 3
+print "$b\n"; // prints 3
+
+$a = 4; // change $a
+
+print "$a\n"; // prints 4
+print "$b\n"; // prints 4 as well, since $b is a reference to $a, which has
+ // been changed
+?>
+]]>
+ </programlisting>
+ </example>
+ </para>
+ <para>
+ As of PHP 5, the <link linkend="language.oop5.basic.new">new</link>
+ operator returns a reference automatically, so assigning the result of
+ <link linkend="language.oop5.basic.new">new</link> by reference results
+ in an <constant>E_DEPRECATED</constant> message in PHP 5.3 and later, and
+ an <constant>E_STRICT</constant> message in earlier versions.
+ </para>
+ <para>
+ For example, this code will result in a warning:
+ <informalexample>
+ <programlisting role="php">
+<![CDATA[
+<?php
+class C {}
+
+/* The following line generates the following error message:
+ * Deprecated: Assigning the return value of new by reference is deprecated in...
+ */
+$o = &new C;
+?>
+]]>
+ </programlisting>
+ </informalexample>
+ </para>
+ <para>
+ More information on references and their potential uses can be found in
+ the <link linkend="language.references">References Explained</link>
+ section of the manual.
+ </para>
+ </sect2>
</sect1>
<sect1 xml:id="language.operators.bitwise">
Modified: phpdoc/en/trunk/language/references.xml
===================================================================
--- phpdoc/en/trunk/language/references.xml 2010-10-22 07:41:25 UTC (rev 304604)
+++ phpdoc/en/trunk/language/references.xml 2010-10-22 08:15:45 UTC (rev 304605)
@@ -105,7 +105,8 @@
Since PHP 5, <link linkend="language.oop5.basic.new">new</link>
returns a reference automatically, so
using <literal>=&</literal> in this context is deprecated and
- produces an <constant>E_STRICT</constant> message.
+ produces an <constant>E_DEPRECATED</constant> message in PHP 5.3 and
+ later, and an <constant>E_STRICT</constant> message in earlier versions.
</para>
<warning>
<para>