php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33679 Function returning reference via "return new" or delegating
Submitted: 2005-07-13 15:27 UTC Modified: 2005-07-13 19:03 UTC
From: mikael at chl dot chalmers dot se Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.4.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mikael at chl dot chalmers dot se
New email:
PHP Version: OS:

 

 [2005-07-13 15:27 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-13 19:03 UTC] sniper@php.net
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


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 05 20:00:01 2025 UTC