![]() |
![]() |
![]() |
||||
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. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
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); ?> |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
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. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 | |
Confirmed User
Join Date: Feb 2004
Location: Right next to you....
Posts: 1,148
|
Quote:
Its just that its better to know some wellknown patterns. |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Industry Role:
Join Date: Aug 2004
Location: Portland, Oregon
Posts: 716
|
Perhaps you should change self::$instancehahahahanull to self::$instance?
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
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 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(); Code:
$_SESSION['cart'][$pid] +1;
__________________
subarus. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
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
|
![]() |
![]() ![]() ![]() ![]() ![]() |