Changing the $database to $db will throw up errors because it is using a protected instance from the basket class.
Can you normally run SQL queries within a destruct?
wishlist class:
<?php
class wishlist extends basket
{
private $wishlist;
private static $instance;
private function __construct()
{
//connect to the database
$this->db = database::getinstance();
$this->items = array();
//retrive the users wishlist
if ($results = $this->database->query("SELECT `wishlist` FROM `members` WHERE id='".$_SESSION['uid']."'"))
{
//return the result into $row
$row = $results->fetch_array(MYSQLI_ASSOC);
//if the wishlist is populated
if (!empty($row['wishlist']))
{
//explode the wishlist into an array
$this->wishlist = explode(',', $row['wishlist']);
//loop the exploded wishlist out into the items format
foreach ($this->wishlist as $name => $value)
{
$this->items[$value] = '1';
}
}
}
}
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()
{
if ($this->items)
{
//set the wishlist to an array to ensure it is empty
$this->wishlist = array();
//loop through the items object to reformat the array into wishlist
foreach ($this->items as $name => $value)
{
$this->wishlist[] = $name;
}
//implode the wishlist
$this->wishlist = implode(',', $this->wishlist);
//save the wishlist to the users wishlist field
$query = "UPDATE `members` SET wishlist='".$this->wishlist."' WHERE id='".$_SESSION['uid']."'";
$this->database->query($query);
}
}
}
?>
relevant sections of basket class
<?php
session_start();
class basket
{
protected $items;
private $quantity;
protected $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;
}
public function countitems()
{
return count($this->items);
}
function __destruct()
{
$_SESSION['items'] = $this->items;
}
}
?>
product page calling the wishlist->add() function
if ($_POST['wishlist'])
{
$wishlist = wishlist::getinstance();
if ($wishlist->add($_POST['item']))
{
//header("Location: ".$_SERVER['HTTP_REFERER']);
exit();
}
else
{
//header("Location: /error/3/");
exit();
}
}