Req #33510 [Opn->Wfx]: Return by reference is redundant

From: Date: Tue, 29 Mar 2016 21:09:53 +0000
Subject: Req #33510 [Opn->Wfx]: Return by reference is redundant
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200234@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: nikic@php.net Reported by: php at pollensoft dot com Summary: Return by reference is redundant -Status: Open +Status: Wont fix Type: Feature/Change Request Package: *General Issues Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2014-07-13 01:43:29] 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#200234) next »