Currently I have a basket class which performs all the methods to control my basket within an e-commerce system I am building.
Another feature I am implementing is a wish list with the functionality being pretty much the same as the basket leaving me with 2 options extend the basket class into the wishlist class or just copy the code i need and edit it into a new class->wishlist.
The problem I can currently see happening though is the wishlist and basket sessions colliding.
with the constructor and destructor setting the sessions items within the database class this could cause issue.
Its probably a stupid question but if i don't call the basket constructor within the wishlist class will the constructor of this extended class not be run but still allowing me access to the methods of the basket class?
class basket
<?php
session_start();
class basket
{
private $items;
private $quantity;
private $database;
private static $instance = NULL;
private function __construct()
{
//create an instance of the database class and connect to the database
$this->database = database::getinstance();
//load the session items into the property $this->items
$this->items = $_SESSION['items'];
}
public function getinstance()
{
if(self::$instance === null)
{
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
//prevent clone
public function __clone()
{
throw new Exception("Cannot clone ".__CLASS__." class");
}
/*////
//Add the item to the $basket->items property after checking is valid
*/////
public function add($id)
{
//check the value isn't an invalid format
if (!preg_match("/^[A-Z0-9]+$/", $id))
{
return 0;
}
//SELECT * FROM `products` WHERE code='$id'
if ($results = $this->database->query("SELECT * FROM `products` WHERE id='$id'"));
{
//if the product is in the database
if ($results->num_rows == 1)
{
//clear the results
$results->close();
if ($this->items[$id] < 1)
{
$this->items[$id] = 1;
}
return 1;
}
else
{
return 0;
}
}
}
function remove($id)
{
if ($this->items[$id])
{
unset($this->items[$id]);
return 1;
}
}
function getitems()
{
if ($this->items)
{
foreach ($this->items as $name => $value)
{
if (empty($where))
{
$where = "'$name'";
}
else
{
$where .= ", '$name'";
}
}
return $this->database->query("SELECT * FROM `products` WHERE `id` IN ($where)");
}
return 0;
}
/*
some other methods i removed for clarity
*/
function __destruct()
{
$_SESSION['items'] = $this->items;
}
}
?>
wishlist clas
<?php
class wishlist extends bakset
{
private static $instance;
private function __construct()
{
$this->database = database::getinstance();
$this->items = //retrive the wishlist items from the database
}
public function getinstance()
{
if(self::$instance === null)
{
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
//prevent clone
public function __clone()
{
throw new Exception("Cannot clone ".__CLASS__." class");
}
public function __destruct()
{
//save the wishlist items into the database = $this->items
}
}
?>
I'm still getting parts of the OO theory round my head but im getting there.
thanks in advance.