Re: Object disappearing across pages
| From: | Sheila Myers | Date: | Wed, 17 Oct 2001 22:52:19 +0000 |
| Subject: | Re: Object disappearing across pages | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71417@lists.php.net to get a copy of this message | ||
Just answered my own question. If I define the $sess_cart as global in
the main.php which calls the methods, it works.
Thanks for the suggestions.
Sheila Myers wrote:
>
> Yes, I'm defining the class then starting the session. Should teh
> session be started in the class definition also? I've tried it with and
> without and it doesn't seem to make much difference.
>
> Something is definitely wrong with how these variables are getting
> defined and manipulated. I looked at the session file and saw this:
>
> !sess_login|sess_cart|O:4:"cart":2:{s:7:"cart_id";s:32:"35634fcab6ae6f1730db1942065b563f";s:5:"items";s:0:"";}
>
> (sess_login is another variable I anticipate using, but I'm not using it
> yet). So the variable names are there, and the cart_id has been set,
> but the add method I'm using doesn't seem to put anything in
> $this->items.
>
> I've read something about defining all session variables as global
> within the functions that call them; does this need to be done?
>
> Chris Lee wrote:
> >
> > make sure you are defining the class THEN starting the session, its very
> > important the class is defined FIRST. I dont knwo if this is the problem or
> > not, but it has pissed me off before. another place to look is in the acual
> > session file, find out there the session files are stored, is the data your
> > expecting in there ?
> >
> > --
> >
> > Chris Lee
> > lee@mediawaveonline.com
> >
> > "Sheila Myers" <sheila@consys.com> wrote in message
> > news:3BCDE69F.F8379EE9@consys.com...
> > > Still working The Shopping Cart From Hell (tm).
> > >
> > > My cart object is very simple:
> > > var $items;
> > > var $cart_id;
> > >
> > > function
> > > add($sell_price,$qty,$shipping,$tax,$productid,$short_desc,$note)
> > > {
> > > echo "current ".$this->cart_id;
> > > echo "current".$this->items;
> > > if (isset($this->items[$productid])) //if it is already in the cart,
> > > don't add a new occurrence, just update the total quantity
> > > {
> > > $this->items[$productid]["quantity"] += $qty;
> > > }
> > > else
> > > {
> > > $this->items[$productid]=array(
> > > "price"=>$sell_price,
> > > "quantity"=>$qty,
> > > "shipping"=>$shipping,
> > > "tax"=>$tax,
> > > "desc"=>$short_desc,
> > > "note"=>$note);
> > > }
> > > } //end add
> > > //a lot of other methods
> > > }//end class Cart
> > >
> > > The object is defined as a session variable in an include file, which is
> > > required_once in all scripts. The class definition is required_once
> > > also.
> > >
> > > There is a main.php which handles all the user interaction, db lookups
> > > etc, and passes the info on to the cart methods. What's happening is
> > > that the cart object seems to be destroyed every time, although it's not
> > > hitting the code that creates a new cart. The property $this->cart_id
> > > stays set all through, but if I try to echo $this->items, I get:
> > > 'Undefined property: items' in both the cart.php and the main.php (where
> > > I call $sess_cart->items).
> > >
> > > This is driving me insane because I know it should be very very simple.
> > > I've read all the online stuff I can find and this should work without
> > > having to serialize/unserialize (I'm using php 4.06). Can anyone spot
> > > something I'm doing wrong?
> > >
> > > Thanks,
> > > Sheila