I've read this (References Explained), this stackoverflow (I say this to avoid duplicates and to show I've googled a lot before posting) and I still don't understand how to implement a very simple thing.
Here's what I want to do:
- class Products
- class Product
All with getter / setter.
I have a page that will construct dynamically an array of products, a group of products, then I'll create new products, and add them to that group:
<?php
$group_of_products = new Products();
$product1 = new Product();
$product1->setName("name1");
$product2 = new Product();
$product2->setName("name2");
$product3 = new Product();
$product3->setName("name3");
$group_of_products->addProduct($product1);
$group_of_products->addProduct($product2);
$group_of_products->addProduct($product3);
?>
Now here's what I want:
<?php
$group_of_products->getProduct( 0 )->setName("NewName1");
$group_of_products->getProduct( 1 )->setName("NewName2");
$group_of_products->getProduct( 2 )->setName("NewName3");
echo $product1->getName().", ";
echo $product2->getName().", ";
echo $product3->getName();
?>
And I'd like to see NewName1, NewName2, NewName3
.
How should I declare the function addProduct()
of the class Products
to obtain such behavior?