#33679 [Opn->Bgs]: Function returning reference via "return new" or delegating

From: Date: Wed, 13 Jul 2005 17:03:07 +0000
Subject: #33679 [Opn->Bgs]: Function returning reference via "return new" or delegating
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-81813@lists.php.net to get a copy of this message
ID: 33679 Updated by: sniper@php.net Reported By: mikael at chl dot chalmers dot se -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: Linux PHP Version: 4.4.0 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Previous Comments: ------------------------------------------------------------------------ [2005-07-13 15:27:44] mikael at chl dot chalmers dot se Description: ------------ Would have added a comment to #33558 When calling a function that returns a reference using "return new SomeClass()" (or via delegate to another function that returns a reference) the "Notice: Only variable references should be returned by reference" is triggered. However; when a reference to $this is set in the $GLOBALS array in the contructor the notice is not triggered and both $a and $b refer to the same object as shown in the example. Se also related #24687 and #26439 that gives weight to the desired behaviour of "return new" and "return deletegatingFactoryMethod()" really returning references "return null" being valid from a return-by-reference function is also desired Reproduce code: --------------- class FooBar { function FooBar() { // Enabling this line will prevent the E_NOTICE //$GLOBALS['a'] =& $this; } } function &factory() { return new FooBar(); } function &deletegatingFactoryMethod() { return factory(); } function &returnNull() { return null; } deletegatingFactoryMethod(); returnNull(); $b =& factory(); $b->foo = 'bar'; //var_dump($GLOBALS['a']); var_dump($b); Expected result: ---------------- No notices Actual result: -------------- The notice is triggered 4 times ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=33679&edit=1

« previous php.bugs (#81813) next »