Req #33510 [Opn]: Return by reference is redundant
| From: | yohgaki@php.net | Date: | Sun, 13 Jul 2014 01:43:30 +0000 |
| Subject: | Req #33510 [Opn]: Return by reference is redundant | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186584@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=33510&edit=1
ID: 33510
Updated by: yohgaki@php.net
Reported by: php at pollensoft dot com
Summary: Return by reference is redundant
Status: Open
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
-Operating System: Gentoo Linux
+Operating System: *
-PHP Version: 4.3.11
+PHP Version: *
Block user comment: N
Private report: N
New Comment:
Objects use handles now. So it's reference by default.
It seems
$v = &func();
does not have any meaningful usage, does it?
<?php
function &f1() {
static $v = 0;
return $v;
}
function f2() {
static $v = 0;
return $v;
}
$v = &f1();$v++;var_dump(f1()); // Strict Error
$v = &f2();$v++;var_dump(f2()); // No effect. Value
$v = f1();$v++;var_dump(f1()); // Reference
$v = f2();$v++;var_dump(f2()); // Usual value.
Previous Comments:
------------------------------------------------------------------------
[2005-06-29 23:48:20] php at pollensoft dot com
Description:
------------
This is a feature request.
Return by reference is redundant, see code examples below.
In order to return by reference, you must assign by reference. Return by reference syntax has no
effect on what is returned.
Understanding that this is the expected behaviour ($me->RTFM() == true), one or the other should
be sufficient to retrieve by reference.
if
get is executed with or without the return by reference definition function
&get() the result is the same (it's always a reference).
function &functionName() does nothing unless the assignment is by reference --
ever.
When doing this in an OOP method, &functionName() should be all that is necessary
to return the reference to ensure that a developer using a class doesn't need to remember to
assign the value as a reference.
We mind as well use the language structures you've provided us with :) I would suggest either
making it work or removing return by reference from all of the documentation..
See also
http://bugs.php.net/bug.php?id=9453.
http://bugs.php.net/bug.php?id=29877
Reproduce code:
---------------
class A {
function A() {
$this->arr = array();
}
function &getref() {
return $this->arr;
}
function getval() {
return $this->arr;
}
}
$b = new A;
$c =& $b->getref(); // $c = reference (expected)
$c =& $b->getval(); // $c = reference (expected)
$c = $b->getval(); // $c = value (expected)
$c = $b->getref(); // $c = value (redundant)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=33510&edit=1