470,862 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

...But I thought PHP was call-by-value???

Hi,

I've got this really annoying problem, which I think might be a bug,
but I wanted to check that I'm not missing some subtle PHP feature.

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!

Here's the reduced code:

//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);

This is most annoying as I haven't declared a single reference
anywhere, and yet I'm having to deal with PHP refrence bugs.

HELP! Am I being silly, or is this a MAJOR bug in PHP's call-by-value
architecture?

Any feedback would be much appreciated.

Rob Long.
Jul 17 '05 #1
2 2733
Rob Long wrote:
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!


Have you read this page (PHP manual):

http://uk.php.net/manual/en/language.oop.newref.php

It talks all about the confusion of references and copies of objects.
Damn, PHP's class system is weird. I'm glad that nothing I've done has
needed OOP.

Actually, I'm not sure it applies to your code. You don't seem to be
creating a reference in your constructor, you seem to be creating a copy.

Man, this is confusing stuff.
--
Bob
London, UK
echo Mail fefsensmrrjyaheeoceoq\! | tr "jefroq\!" "@obe.uk"
Jul 17 '05 #2
Thanks for your reply.

Yeah, I know there are some intricate and subtle features when it
comes to the use of references within PHP, but what I'm so puzzled
about is that I haven't used a single reference anywhere in my code!
All I'm asking for is bog-standard call-by-value behaviour!

Hmmmph, I'm wondering why on earth this issue hasn't arisen before?

Does no one use PHP for simple OOP?

Robert Downes wrote:
Have you read this page (PHP manual):

http://uk.php.net/manual/en/language.oop.newref.php

It talks all about the confusion of references and copies of objects.
Damn, PHP's class system is weird. I'm glad that nothing I've done has
needed OOP.

Actually, I'm not sure it applies to your code. You don't seem to be
creating a reference in your constructor, you seem to be creating a copy.

Man, this is confusing stuff.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Rob Bolton | last post: by
22 posts views Thread by Charles Law | last post: by
14 posts views Thread by EJ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.