php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34193 return new Object() in function returning reference.
Submitted: 2005-08-19 15:13 UTC Modified: 2005-08-19 15:15 UTC
From: benoit at bh-services dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.4.0 OS: WIN32 XP SP2
Private report: No CVE-ID: None
 [2005-08-19 15:13 UTC] benoit at bh-services dot net
Description:
------------
Hi all,

I've read and searched all the bugs report about the famous "Only variable references should be returned by reference" Notice and I not yet found this case.

This one in fact is very tricky, it happens when you have a function intended to return a reference on a variable, and in this one in some cases you want to return an object (In my case is to manage new Exception() objects to emulate the try/catch mechanism).

So if you try to return directly  new Object() in the function then you have a notice.
This means new Object() does not return a reference (or is not considered as a reference on variable).

By the way, you can do stuff like this :
$obj = & new Object();

So that means here the new Object() is ok for references...

So what is the rule here ?
For me it's simply to allow the usage of references on new Object() calls because is very useful, and for me this not a bug to have reference on new object (which is infact the first reference an object/variable can have).

Thanks for the team, and I hope this one will be fixed in the 4.4.1 ;)

/Benoit Heinrich

Reproduce code:
---------------
<?php
error_reporting(E_ALL); // very important.

class Object()
{
}

function &fct()
{
	return new Object();
}


$obj = & fct(); // will display the notice

$obj = & new Object(); // this one no notice
?>

will display:
Notice: Only variable references should be returned by reference


Expected result:
----------------
Have no notice at all when I use the E_ALL for reporting.

Actual result:
--------------
The famous Notice: Only variable references should be returned by reference.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-19 15:15 UTC] derick@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-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 20:00:02 2026 UTC