The life of a PHP instance?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
svdelle
Forum Newbie
Posts: 6
Joined: Wed Oct 19, 2005 7:40 am

The life of a PHP instance?

Post by svdelle »

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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: The life of a PHP instance?

Post by Jenk »

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.
svdelle
Forum Newbie
Posts: 6
Joined: Wed Oct 19, 2005 7:40 am

Re: The life of a PHP instance?

Post by svdelle »

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?

Post by alex.barylski »

So, a $_SESSION variable is the only solution for a shopping cart?
No

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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: The life of a PHP instance?

Post by pickle »

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.
svdelle
Forum Newbie
Posts: 6
Joined: Wed Oct 19, 2005 7:40 am

Re: The life of a PHP instance?

Post by svdelle »

Thanks guys.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: The life of a PHP instance?

Post by josh »

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
User avatar
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?

Post by Christopher »

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.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: The life of a PHP instance?

Post by josh »

arborint wrote:PS - typically the shopping cart contents are removed upon a succful checkout to prevent repurchasing and allow further shopping.
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 :-D )
User avatar
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?

Post by Christopher »

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 :-D )
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).
(#10850)
svdelle
Forum Newbie
Posts: 6
Joined: Wed Oct 19, 2005 7:40 am

Re: The life of a PHP instance?

Post by svdelle »

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.
Post Reply