You are here: Re: Arrays and Pointers « PHP Programming Language « IT news, forums, messages
Re: Arrays and Pointers

Posted by Oli Filth on 09/29/05 21:49

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация