Doc #53117 [Asn->Csd]: Assignment by reference within assignment operator
| From: | aharvey@php.net | Date: | Fri, 22 Oct 2010 08:17:42 +0000 |
| Subject: | Doc #53117 [Asn->Csd]: Assignment by reference within assignment operator | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-5323@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53117&edit=1
ID: 53117
Updated by: aharvey@php.net
Reported by: dukeofgaming at gmail dot com
Summary: Assignment by reference within assignment operator
-Status: Assigned
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: Any (Windows 7)
PHP Version: 5.2.14
Assigned To: aharvey
Block user comment: N
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
That should be a distinct improvement. I've left the bit in about
objects being assigned by reference -- I know it's not strictly true,
but it's an accurate enough representation of the actual behaviour,
and users who are that interested in how objects are passed are
likely to end up finding Sara's blog on the subject. (We could add a
See Also section linking to that blog, but I think it might only
confuse matters further.)
Previous Comments:
------------------------------------------------------------------------
[2010-10-22 10:15:47] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
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.
------------------------------------------------------------------------
[2010-10-21 02:06:30] cataphract@php.net
It's not deprecated. What's deprecated is "=& new something()".
The documentation page for "Assignment operators" talks briefly about
"=&", but it could use some improvements (it even includes the somewhat
inaccurate statement that in PHP 5 objects are assigned by reference).
------------------------------------------------------------------------
[2010-10-21 00:57:30] dukeofgaming at gmail dot com
Hi again,
Thanks for the detailed follow-up. I did not have the slightest idea
that this
form of using '&' was deprecated. If '=&' is formally an operator,
shouldn't it be
in the "Assignment Operators" section of the documentation?.
Regards
------------------------------------------------------------------------
[2010-10-21 00:39:15] cataphract@php.net
> I am not under the impresion that there is an asign-by-reference
> operator as such, since one is free to separate "=" from "&", where
> "&" indicates return by reference.
Not really. See the parser definitions here:
http://lxr.php.net/xref/PHP_5_3/Zend/zend_language_parser.y#expr_without_variable
The only (non-deprecated) form of an assignment by reference is:
variable '=' '&' variable { zend_check_writable_variable(&$1);
zend_do_end_variable_parse(&$4, BP_VAR_W, 1 TSRMLS_CC);
zend_do_end_variable_parse(&$1, BP_VAR_W, 0 TSRMLS_CC);
zend_do_assign_ref(&$$, &$1, &$4 TSRMLS_CC); }
The fact you can separate "=" and "&" is maybe unfortunate, because it
causes the sort of confusion you have. Although "&" is used in several
similar contexts (pass by reference, return by reference, assign by
reference, include reference in array expression) and they all share a
common implementation pattern (what in PHP is sadly called "references",
which is actually just a "no copy flag"), they are still conceptually
different processes, with some dissimilarities.
In particular, in PHP there are different opcodes for assignment
(operator =) and assignment by reference (operator =&). To assign by
reference, you don't need just a "reference" (a variable that has the
is_ref no copy flag) or something from which we can extract a reference
on the right hand side, we need to actually use the "assign by
reference" operator.
Consider:
$a =& $b;
$c = $a; //not an assignment by reference, but $a is a "reference".
function &h() { return $GLOBALS['b']; }
$c = h(); //not an assignment by reference, but h() returns by reference
------------------------------------------------------------------------
[2010-10-20 23:24:28] dukeofgaming at gmail dot com
I am not under the impresion that there is an asign-by-reference
operator as such,
since one is free to separate "=" from "&", where "&"
indicates return
by
reference. However the expression part makes this clear.
Regards,
David Vega
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/bug.php?id=53117
--
Edit this bug report at http://bugs.php.net/bug.php?id=53117&edit=1