It's more or less like a shopping cart, and I'm having trouble finding a way to send multiple products to the class so it generates multiple instances with it's own values.
I tought about managing each product as arrays. But I have trouble implementing that into the class and the way to input the information.
Basically, I need my class to list me "N" number of products with it's description and cost.
The code below works for a single instance. I think I should give any "new Product" a array indexing or something like:
$n1 = new Product('Cost' => '30', 'Description' => 'My first Product', 'Amount' => '2');
and then I shall define class variable attributes as arrays. But can't manage to do so. I need help.
Code: Select all
<?PHP
class Producto
{
//i think i shall declare this variables as $cantidad=array(); $precio=array(); and so on...
private $cantidad;
private $precio;
private $descripcion;
function __construct($cantidad,$precio,$descripcion)
{
//here i believe i shall use a foreach function and set $this->cantidad[] as $cantidad but tryied so and did not work
$this->cantidad = $cantidad;
$this->precio = $precio;
$this->descripcion = $descripcion;
}
function getCuantos()
{
return $this->cantidad;
}
function getPrecio()
{
return $this->precio;
}
function getDescripcion()
{
return $this->descripcion;
}
}
// what i want is to do not have to type all this code below for EACH product that i want to add.
// there's gotta be a better method.
$creaProducto = new Producto('2','30','lo que sea');
echo $creaProducto->getCuantos()."<BR>";
echo $creaProducto->getPrecio()."<BR>";
echo $creaProducto->getDescripcion()."<BR>";
?>Thanks a lot.