Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:36:38+00:00 2026-06-08T05:36:38+00:00

With the use of static variables and the singleton pattern, I thought that it

  • 0

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(); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:36:39+00:00Added an answer on June 8, 2026 at 5:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I heard that static methods should use only static variables in java. But, main
A colleague of mine told me that I should never use static variables because
I do know that in java static methods can only use static variables and
Time and again, I read that we are not supposed to use static variables
Why cant i use static variables in events handling methods like this private void
Is it reasonable to use private static variables to establish invariants in your class?
I have a static variable that I use as a counter and a non-static
I need a static variable that points to itself to use when referencing the
Possible Duplicate: When to Use Static Classes in C# Questions in the title..........i would
I know that you can use static libraries to manage dependencies for iOS apps.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.