Arrays and Pointers



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...

if I redeclare the object ($nr = new news_resource(); ) again, before
calling the funciton then it gets added in fine.

I'm sure its a typical behaviour and is to do with pointers - but I
don't understand why reassiging the object ($obj = new my_class() )
before callings its funciton should break the link to the original
object (and maintain its state).

In my mind it should either always put a copy in the array or else
always be a pointer.

Can anyone explain why PHP works this way?

Code snippet below:

$nr = new news_resource();

$nr->params_init( 0, "www.num1.com", "Link to num1", "This links to
number 1", 0, 0 );
$resources[] = $nr;

$nr = new news_resource(); //why does this mean that the code works
and the array keeps a copy of the object instead of a pointer?
$nr->params_init( 0, "www.num2.com", "Link to num2", "This links to
number 2", 0, 0 );
$resources[] = $nr;

$nr = new news_resource();
$nr->params_init( 0, "www.num3.com", "Link to num3", "This links to
number 3", 0, 0 );
$resources[] = $nr;



Regards,
Rick Huby
www.e-connected.com

.



Relevant Pages

  • Array Problem
    ... I thought I understood how to traverse an array, but I guess I was wrong. ... have tried writing the code snippet below as: while, for, foreach... ... If it is <= $maxLength do nothing, ...
    (comp.lang.php)
  • Array Problem
    ... I thought I understood how to traverse an array, but I guess I was wrong. ... have tried writing the code snippet below as: while, for, foreach... ... If it is <= $maxLength do nothing, ...
    (alt.php)
  • Re: Workbooks collection is empty even though the some of the document is open
    ... used in VB/A to return, say, an array of object ref's to all Excel ... Peter T ... > your code snippet and see if I can help you with it. ...
    (microsoft.public.excel.programming)
  • Re: Array Problem
    ... > I thought I understood how to traverse an array, but I guess I was wrong. ... > have tried writing the code snippet below as: ...
    (alt.php)
  • Re: Array Problem
    ... > I thought I understood how to traverse an array, but I guess I was wrong. ... > have tried writing the code snippet below as: ...
    (comp.lang.php)