php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33884 "Only variable references should be returned by reference" if I return nothing
Submitted: 2005-07-27 15:02 UTC Modified: 2005-07-29 21:46 UTC
From: php-bug-33884 at ryandesign dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.4.0 OS: N/A
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php-bug-33884 at ryandesign dot com
New email:
PHP Version: OS:

 

 [2005-07-27 15:02 UTC] php-bug-33884 at ryandesign dot com
Description:
------------
Hi. After upgrading from PHP 4.3.11 to 4.4.0 I get the 
famous "Only variable references should be returned by 
reference" notice in one of my projects. The function in 
question returns by reference, but only needs to do this 
sometimes. Other times, it returns nothing at all, because 
the caller does not need a return value. In these latter 
cases, PHP produces the notice as of 4.4.0.

This surprised me because it's perfectly fine to have a 
normal return-by-copy function that does not return 
anything. So why not a return-by-reference function?

I couldn't find any documentation that if your function 
returns by reference, then you must always return something.

In my particular case I can rewrite the function so that it 
always returns something, even when the caller has no use 
for it. I just wasn't sure if the notice in this case was 
intended, and if so, why.

If the behavior is intended, then the documentation should 
reflect this.

Reproduce code:
---------------
error_reporting(E_ALL);

function &foo_by_reference_with_return() {
	echo __FUNCTION__ . "<br />\n";
	return $GLOBALS['bar'];
}
function &foo_by_reference_without_return() {
	echo __FUNCTION__ . "<br />\n";
} // line 11
function foo_by_copy_with_return() {
	echo __FUNCTION__ . "<br />\n";
	return $GLOBALS['bar'];
}
function foo_by_copy_without_return() {
	echo __FUNCTION__ . "<br />\n";
}

foo_by_reference_with_return();
foo_by_reference_without_return(); // causes notice in 4.4.0
foo_by_copy_with_return();
foo_by_copy_without_return();

Expected result:
----------------
foo_by_reference_with_return
foo_by_reference_without_return
foo_by_copy_with_return
foo_by_copy_without_return

Actual result:
--------------
foo_by_reference_with_return
foo_by_reference_without_return
Notice: Only variable references should be returned by 
reference in references.php on line 11
foo_by_copy_with_return
foo_by_copy_without_return

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-29 18:12 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 [2005-07-29 21:46 UTC] sniper@php.net
Hint: Try SEARCH to find the duplicate report..

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 03:01:27 2024 UTC