The life of a PHP instance?
Moderator: General Moderators
The life of a PHP instance?
Hi,
I'm moving into PHP OOP programming for a shop-design. Being used to Flash where you create an instance of a class, and that instance lives throughout the application, I experience that when you create an instance in PHP, that instance is destroyed and recreated as you refresh the page in the browser.
Is the only solution to store instances in a $_SESSION-variable to make it accessible throughout an application or have I missed out on a basic but very important concept in PHP?
Imagine a cart class instance that you have to constantly update no matter how the user browse your website.
I'm moving into PHP OOP programming for a shop-design. Being used to Flash where you create an instance of a class, and that instance lives throughout the application, I experience that when you create an instance in PHP, that instance is destroyed and recreated as you refresh the page in the browser.
Is the only solution to store instances in a $_SESSION-variable to make it accessible throughout an application or have I missed out on a basic but very important concept in PHP?
Imagine a cart class instance that you have to constantly update no matter how the user browse your website.
Re: The life of a PHP instance?
Instances are only alive during the request, everything is destroyed once the web server has finished processing the request. Using $_SESSION only means that the object will be serialised and stored on disk, and recreated at the next request for that session.
Re: The life of a PHP instance?
So, a $_SESSION variable is the only solution for a shopping cart?
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: The life of a PHP instance?
NoSo, a $_SESSION variable is the only solution for a shopping cart?
You "could" store cart data in $_COOKIE's. You also do not need to use $_SESSION but the concept of server persistence is the same and is ideal.
Re: The life of a PHP instance?
It's not your only solution, but it is the best.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: The life of a PHP instance?
Thanks guys.
Re: The life of a PHP instance?
Normally I try not to store too much in the session in terms of serialized objects, not saying don't, just saying there's cleaner ways. Wether or not cart data should go in a session is debatable, but for instance instead of serializing the entire cart object to persistent storage, you could have a method that returns just primary keys from the database ( product skus for eg ). Then on subsequent requests these skus would be iterated, and the cart would be re-instantiated. Reason serializing objects can get messy is what happens if you change the names of properties for a class and push code into production, you will encounter some malformed objects with unpredictable results. You have the idea down though it seems. Also remember the class definition must be included prior to resuming session on subsequent page or the object will default to type stdClass
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: The life of a PHP instance?
Remember the length of time these options will persist data:
- Cookie: until expiration date or the user deletes the cookie
- Session: server setting for expiration time or server job that clears session directory
- Database: until record deleted or unbacked-up server crashes.
And the key concept is that some unique ID is generated that is connected with a browser signature.
For a shopping cart there are cases where you may want to store it in a database. For example, I have a client who is interested in statistics on abandoned shopping sessions. Obviously the only way to track that kind of data is to keep the shopping cart in the database. Or if you want the user to be able to return days later and resume shopping.
PS - typically the shopping cart contents are removed upon a succful checkout to prevent repurchasing and allow further shopping.
- Cookie: until expiration date or the user deletes the cookie
- Session: server setting for expiration time or server job that clears session directory
- Database: until record deleted or unbacked-up server crashes.
And the key concept is that some unique ID is generated that is connected with a browser signature.
For a shopping cart there are cases where you may want to store it in a database. For example, I have a client who is interested in statistics on abandoned shopping sessions. Obviously the only way to track that kind of data is to keep the shopping cart in the database. Or if you want the user to be able to return days later and resume shopping.
PS - typically the shopping cart contents are removed upon a succful checkout to prevent repurchasing and allow further shopping.
(#10850)
Re: The life of a PHP instance?
In this case the data would already exist [in the orders table], repurchasing concerns aside ( most clients would love if the system increased repurchase ratesarborint wrote:PS - typically the shopping cart contents are removed upon a succful checkout to prevent repurchasing and allow further shopping.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: The life of a PHP instance?
Sorry, by repurchasing I meant resubmitting the same order twice. When the order is processed the items in the cart are added to the orders table(s).jshpro2 wrote:In this case the data would already exist [in the orders table], repurchasing concerns aside ( most clients would love if the system increased repurchase rates)
(#10850)
Re: The life of a PHP instance?
The database version may sound like a good approach, as statistics could be a nice feature to implement.
Thank you all for sharing your thoughts.
Thank you all for sharing your thoughts.