php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9481 Passing out class property by reference problem
Submitted: 2001-02-27 10:42 UTC Modified: 2001-03-08 10:29 UTC
From: phil at kusala dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4pl1 OS: Linux (Redhat 6.2) on an i686
Private report: No CVE-ID: None
 [2001-02-27 10:42 UTC] phil at kusala dot com
<?
	function xmpdump($xValue, $sLabel="")
	{
		echo "$sLabel<xmp>";
		var_dump($xValue);
		echo "</xmp>";
	}

	class test
	{
		var $sValue;
		var $Count = 0;
	
		function getTest(&$refObj)
		{
			if (isset($this->sValue))
			{
				$refObj = &$this->sValue;
				xmpdump($refObj, "Reference before being passed back");
				return;
			}
			else
			{
				$refObj = "phil".$this->Count;
				$this->Count++;
				$this->sValue = &$refObj;
				xmpdump($refObj, "Value before being passed back");
				return;
			}
		}
	}
	
	$tst = new test();

	$tst->getTest($tstObj0);
	xmpdump($tstObj0, "Value after being passed back");
	
	$tst->getTest($tstObj1);
	xmpdump($tstObj1, "Refence after being passed back");
?>

The dump of $tstObj1 is NULL, despite the dump of $refObj immediately before it in the function working correctly.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-08 06:58 UTC] stas@php.net
This is not a bug. Please read the "References explained"
part in the manual. When you have $a =& $b and you do $a =&
$c, you don't make a and b be references to c, you make only
$a bound to $c and unbound from $b. So when you did $refObj
= &$this->sValue
 $refObj stopped being reference to call parameter and
became reference to $this->sValue. There's no real way to
make "other" referenced value to be rebound to some new value.
 [2001-03-08 10:29 UTC] phil at kusala dot com
You can get round the above problem somewhat using: 

function foo (&$aVar) 
{ 
$aVar['value'] =& $GLOBALS["baz"]; 
} 
foo($bar); 

$bar['value'] is now a reference to $GLOBALS["baz"] as you would expect.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC