php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32840 returning member-variables causes unwanted return by reference
Submitted: 2005-04-26 14:37 UTC Modified: 2005-04-27 13:38 UTC
From: wagner at bonn dot edu Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.11, 5.0.4 OS: *
Private report: No CVE-ID: None
 [2005-04-26 14:37 UTC] wagner at bonn dot edu
Description:
------------
Returning a value of an array or a member-variable causes    
the return to be by reference.  This also happens in PHP 
4.3.x 
Related to http://bugs.php.net/bug.php?id=32789 which   
seems bogusified beyond repair.   

Reproduce code:
---------------
function incr(&$int) {
  return $int++;
}

function f() {
  static $v;
  if (!$v)   
    $v = 1;
  return($v); 
} 

function f2() {
  static $v;
  if (!$v) {
    $v = array();
    $v[0] = 1;
  }
  return($v[0]); 
} 

echo "f1: ".incr(f()).incr(f())."\n"; 
echo "f2: ".incr(f2()).incr(f2())."\n"; 

Expected result:
----------------
f1: 11 
f2: 12 
 
Same behaviour for both functions. 

Actual result:
--------------
f1: 11 
f2: 12 
 
f2 inexplicably returns a reference. 
The manual states that for this to happen, it should be 
necessary to actually use a & in front of the definition 
of the returning function: 
http://de2.php.net/manual/en/language.references.return.php 
"... you have to use & in both places - to indicate that 
you return by-reference, not a copy as usual, and to 
indicate that reference binding, rather than usual 
assignment, should be done ..." 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-26 14:39 UTC] wagner at bonn dot edu
The Expected result should actually look like this: 
--------------------------------------------------- 
f1: 11  
f2: 11
 [2005-04-26 14:55 UTC] wagner at bonn dot edu
probably related to http://bugs.php.net/bug.php?id=32841 
which also shows the broken behaviour for actual member 
variables
 [2005-04-27 12:52 UTC] sniper@php.net
RTFM:   
  
  http://www.php.net/manual/en/language.variables.scope.php

And the part with title:

  "References with global and static variables"
 [2005-04-27 13:38 UTC] wagner at bonn dot edu
So static is implemented through references. Big deal.      
What does that have to do with this bug? 
Why does only one of the two functions in the example      
return a reference, although both use a static variable?      
 
The problem can be reproduced without static or global 
anyway:      
      
Reproduce code:     
---------------     
function incr(&$int) {      
  return $int++;      
}      
      
function f(&$v) {      
  $cache = $v[0];      
  return($cache);       
}       
      
$v = array();      
$v[0] = 1;      
      
echo "f: ".incr(f($v)).incr(f($v))."\n";      
  
Expected result:   
----------------   
f: 11  
return by value, no effect on $v  
  
Actual result:  
--------------  
f: 12  
return by reference  
  
  
------------  
How does incr() get its hands on $v here? f() should    
return a copy of $cache, which should be a copy too.    
This is apparently not a problem with static, but with how    
array (and object) are implemented in the ZE, and I find    
no mention whatsoever in the manual about that, so this    
is at least a documentation problem. 
Would you please take a good look at what I'm trying to 
explain here and reopen the bug?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 06:01:30 2024 UTC