array of objects?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

array of objects?

Post by Ree »

I do not like using this kind of iteration of db results like this (news items in the db):

Code: Select all

while ($row = $rs->nextRow())
{
  echo $row['headline'];
  echo $row['body'];
}
I would prefer putting all news items as objects in an array and then iterate over them like this:

Code: Select all

foreach ($items as $item)
{
  echo $item->getName();
  echo $item->getBody();
}
For me this would be more convenient, as I may also need to do some tricks with the data in the news item object (for example, do some replacing or any other string manipulation).

Is it ok to use object arrays? What if I have 500 objects in the array? Can this method become a performance hit?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

generally it's fine.. my current project can often have over 1000 objects concurrently loaded and being processed. Our server has yet to complain at all about it.. :)
Post Reply