PHP guru, please help. You'll get 30.00 USD$

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • etech
    Confirmed User
    • Feb 2004
    • 1148

    #1

    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
    Confirmed User
    • May 2004
    • 1323

    #2
    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.

    Comment

    • etech
      Confirmed User
      • Feb 2004
      • 1148

      #3
      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.

      Comment

      • lilspup
        Confirmed User
        • Aug 2004
        • 716

        #4
        Perhaps you should change self::$instancehahahahanull to self::$instance?

        Comment

        • calmlikeabomb
          Confirmed User
          • May 2004
          • 1323

          #5
          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.

          Comment

          • etech
            Confirmed User
            • Feb 2004
            • 1148

            #6
            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

            Comment

            Working...