3

I have a class named MyCart.

Class MyCartClass
{
var $MyCart;  

function getCart(){
  return $this->MyCart;
}

function addItem($item){

    if ($this->MyCart){
        $this->MyCart .= ','.$item;
    } 
    else{
        $this->MyCart = $item;
    }

}

};

$globalCart = new MyCartClass; // create an instance of the class

The variable "$MyCart" is a string containing all the items in the cart, separated with a comma.

Now, I save this class to a file named "cart.php" and I include it in another file.

HOWEVER, every time I call the function "addItem", the if statement goes to the else branch, which means that the "$MyCart" variable does not contain the current state of the cart.

Do i need to store the state of my cart into a "session" variable? Cause this way it will be accessible from all files for sure..

I would appreciate any kind of help!

Thanks.

1
  • I can't see anything wrong with that code, provided you are calling it within the same program run (HTTP call, or whatever). Nothing will persist between HTTP calls unless you use a session. Commented Jun 7, 2011 at 13:02

4 Answers 4

5

Don't use a string to store a list. It's just a bad idea all around.

Instead, store $items as an array, and define it in the constructor

class Cart {

    function __construct() {
        $this->items = array();
    }

    function add($item) {
        $this->items[] = $item;
    }

    function save() {
        $SESSION["cart"] = $this->items;
    }

    function get_items_string() {
        return join(",", $this->items);
    }

}

My PHP is a little rusty, but that should get you started.

Sign up to request clarification or add additional context in comments.

3 Comments

the question wasnt' about how to properly store it.
Thank you. So, by storing the result of the "add new item" function into this array is not enough to hold this result in the entire site, right? I definitely need a session. Is that correct. Thank all of you guys for the immediate response!
Yup, definitely use a session, and yes, I know the question wasn't about how to store it, but I thought the OP had the false impression that sessions can only store strings, which isn't true.
2

If you mean saving it between request then yes you need to put it in $_SESSION.

Otherwise within one request you can use your $globalCart and not lose the contents of your $myCart var.

1 Comment

Thank you "yes123". It seems that there is no other way than storing it into $_SESSION I guess.
1

To simplify this just store the data in an array and use serialize and deserialize to store the data:

Class MyCartClass { 
    var $MyCart = array();

    public function __construct(){
        $this->MyCart = deserialize($_SESSION['CART']);
    }

    public function __destruct(){
        $_SESSION['CART'] = serialize($this->MyCart);
    }


    function getCart(){
        return $this->MyCart;
    }

    function addItem($item){
        $this->MyCart[] = $item;   
    }
}

Please note I just wrote this quickly and didn't run it as I don't have access to run it right now. :-)

1 Comment

I'm pretty sure that you don't need to serialise session data.
0

your can even serialise all class to $_SESSION[] ... but generally good idea is to store in session some id - as example user id & store all all other data in mysql. Getting it each time, is needed ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.