php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25484 functions which accept a reference from inside a loop lose their value
Submitted: 2003-09-11 04:20 UTC Modified: 2003-09-18 06:02 UTC
From: nick at itomic dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.0b2 OS: Redhat 9
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: nick at itomic dot com
New email:
PHP Version: OS:

 

 [2003-09-11 04:20 UTC] nick at itomic dot com
Description:
------------
when a class function accepts a reference parameter & stores that reference in the object, if that parameter passed in is created & recreated inside a loop, then the reference that the class points to changes to the last value of the parameter in the loop.

This is confusing behavious, but it is not necessary buggy behavious no PHP's behalf, maybe it just requires better documentation.

I can see the situation where an object needs to be passed a reference to an object, _not_ a copy of a reference (i.e. when dealing with obbject trees, or lists).

In the below example this may differ from when I am passing in some pre-existing variable (i.e. a variable returned from a class iterator), rather than a variable that is created in the loop

Reproduce code:
---------------
<?php
// plz note this is pseudo code

$res = mysql_query(...);

// this part of the code has been tested

while ($row = mysql_fetch_assoc($res))
{
	$foos[] =& new foo($row);
}

foreach($foos as $key => $foo)
{
	var_dump($foos[$key]->_var);
}

class foo
{
	var $_var = NULL;
	function foo(&$var)
	{
		$this->_var =& $var;
	}
}


Expected result:
----------------
something like:
array(2) {
  ["StageID"]=>
  string(1) "1"
  ["StageName"]=>
  string(10) "Incomplete"
}
array(2) {
  ["StageID"]=>
  string(1) "2"
  ["StageName"]=>
  string(8) "Complete"
}
array(2) {
  ["StageID"]=>
  string(1) "3"
  ["StageName"]=>
  string(10) "Not Needed"
}
array(2) {
  ["StageID"]=>
  string(1) "4"
  ["StageName"]=>
  string(7) "Waiting"
}
array(2) {
  ["StageID"]=>
  string(1) "5"
  ["StageName"]=>
  string(9) "Submitted"
}

Actual result:
--------------
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-09-11 04:22 UTC] nick at itomic dot com
sorry the third paragraph should read:

I can see the situation where an object needs to be passed a reference
to an object, _not_ a copy of an object(i.e. when dealing with
obbject trees, or lists).
 [2003-09-11 10:12 UTC] sniper@php.net
ZE2 is in PHP 5. And provide a working example script, not some pseudo code..


 [2003-09-18 06:02 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Feb 05 02:05:48 2023 UTC