php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28397 call-by-value on objects doesn't work
Submitted: 2004-05-14 13:59 UTC Modified: 2005-02-15 10:57 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: bobalong at gmx dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.4 OS: Redhat 9
Private report: No CVE-ID: None
 [2004-05-14 13:59 UTC] bobalong at gmx dot net
Description:
------------
I've created a user-defined list type - a bit like the ubiquitous ArrayList - and wrapped it in another class, which exposes part of the internal list's interface... OK, so nothing radical so far.

My problem is that when I make use of the list's exposed interface in the wrapper class, it becomes a reference type for no apparent reason.

Another consequence, due to a previous bug (http://bugs.php.net/bug.php?id=20993), is that if I pass the wrapper object to a function that modifies the internal list, the original object gets modified too, as though I'd passed it by reference!


Reproduce code:
---------------
<pre>
<?
	//define a user type, similar to an ArrayList
	Class MyList
	{
		var $internalArray = array();

		function Add($obj)
		{
			array_push($this -> internalArray, $obj);
		}

		function Remove($index)
		{
			unset($this -> internalArray[$index]);
		}
	}

	//create a wrapper for the above list
	Class MyListWrapper
	{ 
		var $myList;

		function MyListWrapper()
		{
			$this -> myList = new MyList();
		}

		function AddItem($item)
		{
			$this -> myList -> Add($item);
		}

		function RemoveItem($index)
		{
			$this -> myList -> Remove($index);
		}
	}

	//function that modifies the wrapper's internal list
	function UpdateListWrapper($listWrapper)
	{
		$listWrapper -> RemoveItem(0);
	}

	//1. create a new wrapper object and dump
	$listWrapper = new MyListWrapper();
	var_dump($listWrapper);

	//2. now add item to wrapper object and dump
	$listWrapper -> AddItem("id");
	var_dump($listWrapper); //notice the list is now a reference type

	//3. now pass to modification function
	UpdateListWrapper($listWrapper);

	//4. see the original has been modified, as if 
	//   call-by-reference had been used
	var_dump($listWrapper);
?>
</pre>

Expected result:
----------------
object(mylistwrapper)(1) {
  ["myList"]=>
  object(mylist)(1) {
    ["internalArray"]=>
    array(0) {
    }
  }
}
object(mylistwrapper)(1) {
  ["myList"]=>
  object(mylist)(1) {
    ["internalArray"]=>
    array(1) {
      [0]=>
      string(2) "id"
    }
  }
}
object(mylistwrapper)(1) {
  ["myList"]=>
  object(mylist)(1) {
    ["internalArray"]=>
    array(1) {
      [0]=>
      string(2) "id"
    }
  }
}


Actual result:
--------------
object(mylistwrapper)(1) {
  ["myList"]=>
  object(mylist)(1) {
    ["internalArray"]=>
    array(0) {
    }
  }
}
object(mylistwrapper)(1) {
  ["myList"]=>
  &object(mylist)(1) {
    ["internalArray"]=>
    array(1) {
      [0]=>
      string(2) "id"
    }
  }
}
object(mylistwrapper)(1) {
  ["myList"]=>
  &object(mylist)(1) {
    ["internalArray"]=>
    array(0) {
    }
  }
}



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-11 20:34 UTC] tony2001@php.net
Please provide a *short* but complete reproduce script.
Also, I don't get why it's a ZE2 bug if you're using 4.3.x.
 [2005-02-12 11:56 UTC] bobalong at gmx dot net
Just to clarify, this is NOT a ZE2 bug - my mistake. I can't make the reproduce script much shorter than it is, as it involves class definitions and client code. I could remove the comments, but that's not really what you're talking about, right?

Personally I've moved to PHP5 and found the object model to be absoultely fine. If I were working on PHP4 bugs I would rationalise that anyone looking for this kind of (fairly) advanced OO behaviour would probably be using PHP5 by now anyway.

Cheers
Rob
 [2005-02-15 01:52 UTC] tony2001@php.net
Okay, so let's close this report until someone (you?) is able to provide a shorter and more readable reproduce script, that clearly decribes the problem.
 [2005-02-15 10:57 UTC] bobalong at gmx dot net
Look - like I said before, the reproduce script is as short as I can make it. It's not difficult to get your head around, so if you can't be bothered to take the time then that's your issue. I'm aware of your rules regarding reproduce scripts, but due to the deep-seated nature of the problem, in this case I hold that the reproduce script I submitted is readable and usable.

I'm sure anyone who cares about objects in PHP4 would be interested in this bug, but like I said in my last post, I would imagine at most of those people have moved to PHP5 by now.

Keep up the good work!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 06:01:27 2024 UTC