View Single Post
Old 03-04-2005, 01:05 AM  
etech
Confirmed User
 
Join Date: Feb 2004
Location: Right next to you....
Posts: 1,148
PHP guru, please help. You'll get 30.00 USD$

Im writing a singleton pattern for a shopping basket.
But it keeps creating a new instance of the class when im calling Basket::getInstance();
What do i do wrong, i searched everywhere on google.

30$ to the guy that figure this out.

<?php
class Basket
{
private static $instance = null;
private $liste;

private function __construct()
{

$this->liste = array();
}

public static function &getInstance()
{
if(self::$instancehahahahanull)
{
echo "opretter instance".rand(1,10000);
self::$instance = new Basket();
}

return self::$instance;
}

public function pushProduct($object)
{
array_push($this->liste,$object);
}

public function popProduct($object)
{
$newListe = array();
for($i=0; $i<count($this->liste);$i++)
{
if($this->liste[$i]!=$object)
{
array_push($newListe,$this->liste[$i]);
}
}
$this->liste = $newListe;
}

public function getList()
{
return $this->liste;
}
}
?>

This is my main method

<?php
include_once("class/Basket.php");
$basket = Basket::getInstance();
$basket->pushProduct($object);
?>
etech is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote