php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33510 Return by reference is redundant
Submitted: 2005-06-29 23:48 UTC Modified: 2016-03-29 21:09 UTC
Votes:5
Avg. Score:4.0 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: php at pollensoft dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: * OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-06-29 23:48 UTC] 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)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-13 01:43 UTC] yohgaki@php.net
-Package: Feature/Change Request +Package: *General Issues -Operating System: Gentoo Linux +Operating System: * -PHP Version: 4.3.11 +PHP Version: *
 [2014-07-13 01:43 UTC] yohgaki@php.net
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.
 [2016-03-29 21:09 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2016-03-29 21:09 UTC] nikic@php.net
So, one decade later :) With the new object model in PHP 5, return by reference is barely used anymore. There is only one remotely common use case that I am aware of and that's returning by reference from __get() or ArrayAccess::offsetGet(). This is what allows indirect modifications of overloaded entities, e.g. writing $obj->prop[]=...

That use case should also serve to illustrate why be do not implicitly treat assignments of the result of reference-returning functions as by-reference assignments. If you use overloaded properties and write $a = $obj->prop you will generally not expect $a to be a reference afterwards. Actually using the return value as a reference is only of interest in some specific, narrow situations (like the case of appending to an array), not something you would want as a default behavior.

Closing this request as Won't Fix as this the current behavior is not likely to change.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC