php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #2753 Cannot return references from functions
Submitted: 1999-11-18 13:54 UTC Modified: 2000-06-09 14:37 UTC
From: duncan at emarketeers dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0 Beta 3 OS: Linux 2.2.9 (RedHat 6.0)
Private report: No CVE-ID: None
 [1999-11-18 13:54 UTC] duncan at emarketeers dot com
It is not possible to return a reference from a function, the parser reports

return &$x;

as a syntax error, and using something like

$a=&$x;
return $a;

does not return a reference.

Running 4.0b3 on Apache 1.3.9, compiled with mysql

<?
class tata {
	var $a;
}

class toto {
var $a;
function toto() {
	$this->a=new tata;
	$this->a->a=1;
}
function titi() {
	$b=&$this->a;
	return $b;
}
}
$x=new toto();
$y=&$x->a;
$z=$x->titi();

$y->a=5;
$z->a=6;

echo "Actual:  z->a = $z->a y->a = $y->a x->a->a = ".$x->a->a ."<BR>";
echo "Expected:z->a = 6 y->a = 6 x->a->a = 6 <BR>";
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-11-18 15:13 UTC] andrei at cvs dot php dot net
Not a problem since it's not supported by design.
Moving to feature/change request.
 [2000-06-09 14:37 UTC] hholzgra at cvs dot php dot net
it has been implemented in 12/1999, this was the 
original commit message:

ndi and I finished working on the return references support in Zend.  We 
tested it a bit, and merged it into the main branch.

To return a reference from a function, you must tag the function as a 
function that returns a reference:

function &foo($a, $b, $c)
{
        ...
}

However, $a = foo(...) will still not cause $a to be a reference to the 
returned value (much like $a = $b, when $b is a reference, copies the value 
of $b onto $a, and doesn't make $a to be a reference to $b).  In order to 
make $a be a reference to the returned value, you must use the notation:

$a = &foo();



 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Jun 17 14:00:02 2026 UTC