php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52133 Functions cannot return an argument passed by reference as reference
Submitted: 2010-06-21 13:56 UTC Modified: 2010-07-04 15:48 UTC
From: bschussek at gmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: Linux Ubuntu 10.04
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: bschussek at gmail dot com
New email:
PHP Version: OS:

 

 [2010-06-21 13:56 UTC] bschussek at gmail dot com
Description:
------------
PHP does not behave as expected when passing arguments by reference into a 
function that are passed out again by reference.

IMO, the below test script should behave equivalently to the following code 
(which produces the expected result):

$b = 'foobar';
$a =& $b;
$c =& $a;
$c[2] = 'z';

var_dump($c);
var_dump($b);

Test script:
---------------
function &ref(&$a)
{
  return $a;
}

$b = 'foobar';
$c = ref($b);
$c[2] = 'z';

Expected result:
----------------
string 'fozbar' (length=6)
string 'fozbar' (length=6)

Actual result:
--------------
string 'fozbar' (length=6)
string 'foobar' (length=6)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-23 00:29 UTC] johannes@php.net
-Status: Open +Status: Feedback
 [2010-06-23 00:29 UTC] johannes@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/
 [2010-06-23 00:48 UTC] bschussek at gmail dot com
-Status: Feedback +Status: Open
 [2010-06-23 00:48 UTC] bschussek at gmail dot com
Sorry, but the test script produces still the wrong output for me.

$ php -v
PHP 5.3.3-dev (cli) (built: Jun 23 2010 00:45:57) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans

$ php test.php
string(6) "foobar"
string(6) "fozbar"
 [2010-06-23 00:51 UTC] bschussek at gmail dot com
In my last comment I dumped first $b and then $c, so the output hasn't changed 
from PHP 5.3.2.
 [2010-06-23 00:56 UTC] johannes@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: dmitry
 [2010-06-23 09:31 UTC] dmitry@php.net
-Status: Assigned +Status: Bogus
 [2010-06-23 09:31 UTC] dmitry@php.net
You forgot "&" in assignment.

$c =& ref($b);
 [2010-07-04 15:48 UTC] bschussek at gmail dot com
Thanks, that's right. Maybe this error message should be improved then?

"Strict standards: Only variables should be assigned by reference"

Obviously (which I didn't know, due to the error message) it is possible to 
assign functions by reference, but only if they are marked to return a reference.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 16:01:37 2025 UTC