Re: Arrays and Pointers
- From: Oli Filth <catch@xxxxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 18:49:03 GMT
thehuby said the following on 29/09/2005 15:39:
I have come across an issue using arrays ond objects; I am using an array to keep track of 'n' number of object(The code snippet below is from my initial testing)
I first declare the object ($nr = new news_resource();), then call a function of the object ($nr->params_init(); ). This is the put into the first element of the db, I then call the function again and put the object in the next element array.
However when I print it all out at the end, all the elements of the array store the object with the same values...
Yes, because $nr is a reference to the news_resource object. Every time you add it to the array (with $resources[] = $nr), you're inserting *another* reference to the same object.
i.e.:
$a = new object(); $b = $a;
$b and $a refer to the *same* object. Anything you to do $b will happen to $a, because they're just aliases of each other.
if I redeclare the object ($nr = new news_resource(); ) again, before calling the funciton then it gets added in fine.
In this case, you're redefining $nr as a reference to a new object, so when you do $resources[] = $nr now, you're storing a reference to a new, separate object.
-- Oli .
- References:
- Arrays and Pointers
- From: thehuby
- Arrays and Pointers
- Prev by Date: Re: echo RAW data from unicode page?
- Next by Date: Re: MD5
- Previous by thread: Arrays and Pointers
- Next by thread: Re: Arrays and Pointers
- Index(es):
Relevant Pages
|