Re: Arrays and Pointers



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 .



Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)
  • Re: Perlish map() function
    ... the reference material, the Array constructor was introduced in JavaScript ... evaluation would result in a ReferenceError. ... identifier shows that it is true. ...
    (comp.lang.javascript)