I have an application that has two classes a parent class and a child class. the parent class has a function that retrieves a private array called $storage. the function name is called get_storage()
I have another class that extends off the parent class that holds that function get_storage() which retrieves the $storage array.
if the child class calls the function like this $get_storage()
It doesnt work.
Only way it works is if i throw the parent class object into a parameter of the child class and then store it into the child class property called products and then call it like this
$this->products->get_storage()
the called looks like this
class product_config extends product /* PRODUCT CONFIG CLASS */
{
private $storage = array();
// overwrite the parent's constructor
public function __construct()
{
}
function get_storage(){
return $this->storage;
}
function set_product($name, $desc, $price)
{
$this->storage[] = new product($name, $desc, $price);
}
function test()
{
echo $this->storage[0]->get_product_name();
}
function render_products()
{
if(!$this->storage)
{
return 'There are no products in storage!';
}
else
{
foreach($this->storage as $value)
{
echo $value->get_product_name()." ".$value->get_price()."</br>";
}
echo $total = $value->get_product_name() + $value->get_price();
}
}
}/* PRODUCT CONFIG CLASS END */
above is the parent class and below is the child class
class cart_manager extends product_config { /* CART MANAGER */
private $sql_con;
private $cart = array();
private $total;
private $products;
function __construct($product)
{
$this->products = $product;
$this->total = 0;
foreach($product->get_storage() as $value)
{
$this->total += $value->get_price();
}
}
function get_total() {
return $this->total;
}
function cart_insert($products){
$this->total = 0;
foreach($products as $value){
$this->total += $value->price;
}
}
function display_cart(){ ?>
<table>
<thead>
<th>Name</th><th>Price</th>
</thead>
<?php foreach($this->products->get_storage() as $value){ ?>
<tr>
<td><?php echo $value->get_product_name(); ?></td><td><?php echo $value->get_price(); ?></td>
</tr>
<?php } ?>
<tfoot>
<td></td><td><?php echo $this->total; ?></td>
</tfoot>
</table>
}
}