|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 17:00:01 2025 UTC |
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.