#28397 [NEW]: call-by-value on objects doesn't work
| From: | bobalong at gmx dot net | Date: | Fri, 14 May 2004 11:59:51 +0000 |
| Subject: | #28397 [NEW]: call-by-value on objects doesn't work | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-59618@lists.php.net to get a copy of this message | ||
From: bobalong at gmx dot net
Operating system: Redhat 9
PHP version: 4.3.4
PHP Bug Type: Zend Engine 2 problem
Bug description: call-by-value on objects doesn't work
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) {
}
}
}
--
Edit bug report at http://bugs.php.net/?id=28397&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=28397&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=28397&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=28397&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=28397&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=28397&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=28397&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=28397&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=28397&r=support
Expected behavior: http://bugs.php.net/fix.php?id=28397&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=28397&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=28397&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=28397&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28397&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=28397&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=28397&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=28397&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=28397&r=float