Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 03-04-2005, 01:05 AM   #1
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
Old 03-04-2005, 01:19 AM   #2
calmlikeabomb
Confirmed User
 
calmlikeabomb's Avatar
 
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
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.
__________________
subarus.
calmlikeabomb is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-04-2005, 05:03 AM   #3
etech
Confirmed User
 
Join Date: Feb 2004
Location: Right next to you....
Posts: 1,148
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.
etech is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-04-2005, 07:15 AM   #4
lilspup
Confirmed User
 
Industry Role:
Join Date: Aug 2004
Location: Portland, Oregon
Posts: 716
Perhaps you should change self::$instancehahahahanull to self::$instance?
lilspup is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-04-2005, 12:18 PM   #5
calmlikeabomb
Confirmed User
 
calmlikeabomb's Avatar
 
Join Date: May 2004
Location: SW Palm Bay, Florida
Posts: 1,323
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.
__________________
subarus.
calmlikeabomb is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-05-2005, 10:14 AM   #6
etech
Confirmed User
 
Join Date: Feb 2004
Location: Right next to you....
Posts: 1,148
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
etech is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.