Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton Pattern In PHP.... How Can I Save State Between Requests

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(); 
like image 576
user866190 Avatar asked Jul 24 '12 14:07

user866190


3 Answers

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.

like image 112
phant0m Avatar answered Sep 16 '22 20:09

phant0m


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.

like image 38
markus Avatar answered Sep 16 '22 20:09

markus


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.

like image 45
Marc B Avatar answered Sep 16 '22 20:09

Marc B