Page 1 of 1

How To Create List of Class?

Posted: Sun Feb 22, 2009 12:30 am
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!

Re: How To Create List of Class?

Posted: Sun Feb 22, 2009 12:40 am
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();

Re: How To Create List of Class?

Posted: Tue Feb 24, 2009 7:52 am
by Ek0nomik
Thanks. :)