473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

...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 2847
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

100
by: Peter | last post by:
Company thought DB2 will be better than Oracle. The bottom line is when you do select, the system crash. I think it may take 4-5 years for DB2 to reach Oracle standard. Peter
5
by: tHeRoBeRtMiTcHeLL | last post by:
Well, I think I have bitten off a little more than I can chew (at least all at once), and I'm only trying to hammer out tables/relationships at the design level. Which translates to "Seasoned...
6
by: Rob Bolton | last post by:
Hi there, If a program running under the interactive logon session (say Susan), needs to impersonate Bob (via "LogonUser()"), but Bob needs to access the network as Susan (i.e., his local...
22
by: Charles Law | last post by:
Could someone please explain to me, in words of one syllable or less, how I get the Validating event to fire for a form. I have a form with one text box, and two buttons: OK and Cancel. I have...
8
by: Larry Lard | last post by:
Today I discovered that a syntax that I thought was forbidden in C# but allowed in VB.NET (and I _don't like_ that it's allowed in VB.NET) is actually allowed in C#. Which confused me. The syntax...
4
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
9
by: Cliff | last post by:
I've got a number of SNMP devices scattered around the globe that I want to get some information off.. I've got a couple of classes whcih get a quite complex table together from SQL and SNMP...
14
by: EJ | last post by:
I'm real new to javasscript. The task is to develop a web page which will allow users to customize a laptop ... show the price as they change options ... transfer the price and system description...
6
by: ssecorp | last post by:
def fib(n): def fibt(a, b, n): if n <= 1: return b else: return fibt(b, a + b, n - 1) if n == 0: return 0 else: return fibt(0, 1, n);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.