5

With the use of static variables and the singleton pattern, I thought that it would be easy enough to create a simple shopping cart that remembered what items where in the cart when another page was loaded.

I am having a problem of the shopping cart not remembering what was already in it when the page is refreshed.

Is there a problem with my code below or should I just use globals or a mysql database.

What is the best way to go about storing state..

<?php
//create a singleton class
class shoppingCart {

    private static $_shoppingCartItems = array();
    private static $_instance = null;

    private function __construct(){

    }

    public static function getInstance(){
        if(self::$_instance == null)
            self::$_instance = new shoppingCart();
        return self::$_instance;            
    }


    public function add(ShoppingItem $item){
        $this->_shoppingCartItems[] = $item;
    }

    public function cartCount(){                 
        return count($this->_shoppingCartItems);
    }  
}
?>

Implementation

$item = new shoppingItem();

$shoppingCart = shoppingCart::getInstance();
$shoppingCart->add($item);
$shoppingCart->add($item);

//should increment by 2 on each page load but it doesn't
echo $shoppingCart->cartCount(); 
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
user866190
  • 855
  • 5
  • 14
  • 31

3 Answers3

5

Static class members (or any other variables for that matter) are not preserved across different requests. Never.

Sessions to the rescue

The only exception to this is $_SESSION; which is a special mechanism to allow for just that.

Star the session with session_start() at the top of your script.

You can now use $_SESSION like a regular array to store and retrieve information. A session belongs to a single user, it is not a means of sharing data across all your users.

Have a look here for an introduction.

Silence

You must not output anything before session_start() is called. That is to say, <?php must be the exact first thing in a PHP script that wishes to use sessions. Further, there must be no echo statements or any other output generating functions between <?php and session_start().

Output Buffering

If you really must generate output before starting the session, you can use output buffering.

Notes

  • $_SESSION is forgetful. After a certain time of inactivity on the user's side, the data will be deleted.
  • If you get the following error message, you violated the above guidelines. Another possibility is that your script has a BOM (Unicode byte order mark). If so, remove it.

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at

The reason this happens is due to the way PHP handles output: It tries to get the output as fast as possible to the user. However, the HTTP protocol transmits certain control data (cookies, which session belongs to you etc), called "headers" before all the output ("body") of the response. As soon as you output anything, the headers need to get sent - unless you use output buffering that is.

phant0m
  • 16,595
  • 5
  • 50
  • 82
  • Thanks for the heads up. I am quite familiar with sessions. I was just reading up on design patterns ,OOP and how it would integrate into a real life example. – user866190 Jul 24 '12 at 15:01
  • @user866190 Oh, I see :) markus-tharkun has the right of it, take a look at the post linked by him and all the links therein. – phant0m Jul 24 '12 at 15:04
3

I think I can see your thought pattern there but what you're trying to do is wrong in many ways.

1. Singleton is NOT a pattern, it's an antipattern

The Singleton is an anti-pattern and should be avoided at all costs. See this great answer by Gordon for the why.

2. HTTP is a stateless protocol.

Nothing you do in PHP alone will help you to preserve state across two requests. Your $shoppingCart is created from the scratch for each request, in fact, your whole application is. You should NOT try to persist data in objects instead you should recreate state after every request, by fetching the respective data from somewhere else. In your example probably from some sort of database nosql or sql.

3. Sessions

You can persist user specific data in the superglobal $_SESSION, but in most cases I advice against it. Your user session should hold authentication and user data but you should avoid storing all kinds data relevant for your business logic in there.

Community
  • 1
  • 1
markus
  • 40,136
  • 23
  • 97
  • 142
  • Yeah, I had a quick read of the link that you posted and it seems like there isn't much of a case for singletons in PHP, would this be the same in Python as well..? – user866190 Jul 24 '12 at 15:09
  • I haven't learned Python yet but I think Singletons aren't a very good thing in general: http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ – markus Jul 24 '12 at 15:17
  • Mmmm, and from when singletons did become an anti-pattern ? That is a news to me. I really don't agree, in my prog. life i did see a lot of them in lot of languages, i did use them personally, and yes I did watch lot of bad use cases, but never though of them as an anti pattern. – cybercow Feb 18 '16 at 20:34
  • 1
    Singleton is a well known pattern and has it's applications, it doesn't make it an anti-pattern that PHP don't support application memory. Is just that doesn't make sense to try to implement singleton in PHP, as the link clearly states. – Pablo Pazos Nov 06 '17 at 02:55
1

PHP is not an application server. It will not automatically persist your "application" state between requests. You have to do that yourself via $_SESSION, cookies, and/or your own private methods.

Unless you take steps to preserve data, the state of the application is wiped when the HTTP request that invoked the script(s) is ended.

Marc B
  • 356,200
  • 43
  • 426
  • 500