GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP guru, please help. You'll get 30.00 USD$ (https://gfy.com/showthread.php?t=439756)

etech 03-04-2005 01:05 AM

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);
?>

calmlikeabomb 03-04-2005 01:19 AM

So you are just trying to create a variable that is storing the products and quantity that the user will be purchasing ?

How about using a server side session ?

Let me know if I'm following you here.

etech 03-04-2005 05:03 AM

Quote:

Originally Posted by calmlikeabomb
So you are just trying to create a variable that is storing the products and quantity that the user will be purchasing ?

How about using a server side session ?

Let me know if I'm following you here.

Yes i am, it crossed my mind. and will work ofcourse.
Its just that its better to know some wellknown patterns.

lilspup 03-04-2005 07:15 AM

Perhaps you should change self::$instancehahahahanull to self::$instance? :winkwink:

calmlikeabomb 03-04-2005 12:18 PM

Ok, well let's try a using a session.

This can be your addcart.php or whatever.

http://www.name.com/add.php?id=6

Where 6 is the product we are adding to the basket.

Code:

session_start();// start the session

if (is_numeric ($_GET['pid'])) { // Check for a product ID.

        $pid = $_GET['pid'];// Product ID being added to cart

if (isset ($_SESSION['cart'][$pid])) {// don't allow user to add twice
echo '<p>This item has already been added to your shopping cart!</p>';
exit();
               
} else {// If the product isn't part of the session add it.
$qty = 1;
}

        // Add to the cart session variable.
        $_SESSION['cart'][$pid] = $qty;

}//end if check for a product

That variable will hold all of your product ID's and a quantity of 1.

If you want to allow the item to be added to the cart more than 1 time.
Find and Replace:

Code:

echo '<p>This item has already been added to your shopping cart!</p>';
exit();

with:
Code:

$_SESSION['cart'][$pid] +1;
You can use this variable on your view cart page to retrieve what's in the user cart session.

etech 03-05-2005 10:14 AM

I know it will work with saving the data in a Session. But is it impossible wo get a singleton working ? I dont get it, cause alot of sites is talking about how great it is, that you can make a singleton on php5


All times are GMT -7. The time now is 07:31 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123