How To Create List of Class?

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
Ek0nomik
Forum Newbie
Posts: 5
Joined: Sun Feb 22, 2009 12:24 am

How To Create List of Class?

Post by Ek0nomik »

I am wondering how I can create a list of one of my classes. As an example, I have a class called Product, and it has properties of ProductID, and ProductName. These values get pulled and populated from the database via accessors.

How can I create an object, say, $objProducts that contains a list of class Product. Then, I could loop through $objProducts looking at each individual Product object.

In VB.NET you can do this by: Dim objProducts As New List Of clsProduct. Then I can add instances of clsProduct to this list.

Can I use an ArrayList? Is there something better I can use? I just started using PHP today, but I wasn't able to find anything about this.

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To Create List of Class?

Post by requinix »

You just make an array. PHP doesn't care about what you put in it.

Code: Select all

$array = array();
$array[] = new Product();
$array[] = new Product();
$array[] = 123; // oops, a number. not a problem though
$array[] = new Product();
Ek0nomik
Forum Newbie
Posts: 5
Joined: Sun Feb 22, 2009 12:24 am

Re: How To Create List of Class?

Post by Ek0nomik »

Thanks. :)
Post Reply