ID:               28397
 Updated by:       [EMAIL PROTECTED]
 Reported By:      bobalong at gmx dot net
-Status:           Open
+Status:           Feedback
-Bug Type:         Zend Engine 2 problem
+Bug Type:         Scripting Engine problem
 Operating System: Redhat 9
 PHP Version:      4.3.4
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[2004-05-14 13:59:49] 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) {
    }
  }
}




------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=28397&edit=1

Reply via email to