views:

189

answers:

6

I have been poking around in PHP for OOP and I noticed something... Objects are re-instantiated each time the page is refreshed. The problem is that I want the object to keep certain information in class variables for the whole time that someone is on a website.

  1. Is there some sort of way to keep an object alive the whole time that someone is surfing on the website?
  2. What alternatives are there to my problem?

It would be really helpful to have example too!

Thank you for any help!

+6  A: 

You can use Sessions to keep data associated to one user between different pages (quoting) :

Session support in PHP consists of a way to preserve certain data across subsequent accesses.

See the Session Handling section of the manual, for more informations about sessions.

Pascal MARTIN
I have a little idea now. In my constructor, I will initialize the class variables if it is the first time. Otherwise, I get the data from the $_SESSION array. When the destructor is called I save the class data inside the $_SESSION array. In a way, it feels like the OOP is broken by having to place everything from the object into a global array for each refresh. $_SESSION can be accessed anywhere.
Partial
+2  A: 

A php script has to exit before apache can serve the page, so if you really want to do that, one thing you can do is serialize and store all the objects that you want to persist and use session cookies to keep track of the users

Charles Ma
+1: Nice! I didn't know you could do this in PHP. So I should serialize the object and store that in the $_SESSION array?
Partial
Be aware though that resources can't be serialized, and thus cannot be stored in a session. For instance if the object you store in a session uses a DB connection, it will be gone when it is retrieved from the session again. But PHP has a mechanism (a magic class method) for this: `__wakeup`. When an object gets deserialized `__wakeup` is called. So, in this method you can reactivate the resources again. See the docs about `__wakeup`: http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep
fireeyedboy
Do you really need to serialize objects when storing them in `$_SESSION`? As far as I remember it's done automatically...
Tomas Markauskas
Thomas: you are correct. PHP automatically serializes any objects found in $_SESSION
Frank Farmer
heh, I didn't know about automatic serializing of objects in $_SESSION, interesting :)
Charles Ma
+3  A: 

PHP isn't stateful. Every page load is a one time event. You can persist data with sessions, or by storing information in a database.

Tyler Smith
+2  A: 
  1. PHP isn't statefull every request is a new process on the server

Your best bet is to use session data and hand the session data to the objects when you instantiate them. Have the contructors pull the data they need out of the session, and you'll essentially have the state fullness you need.

you can acess sesion using

$_SESSION['stuff'] = $data;

then you can use your objects like $x = new DataStore($_SESSION['stuff']);

if theres data in the session the object will populate itself from that data. Otherwise it will default to its standard init.

Chris
+1  A: 

Even when approaches like serializing objects and then deserializing them is useful, you have to make sure you understand first why your objects "disappear".

HTTP, the protocol used to retrieve pages and other resources from Web servers, is stateless. It basically means one request knows nothing from another request, even when it came from the same user. Think of it this way, when you request your PHP page, the script is run and after it finishes Apache sends out the result to you. When you request the page again, it does the same thing as if it was the very first time you did it. It's stateless.

There are techniques to keep state between requests (make it to not forget your objects) and those involve things like cookies or URL rewriting. But you have to keep in mind the stateless nature of HTTP (and thus your PHP script) when developing Web applications.

Cesar
Just to provide some extra clarity, HTTP's statelessness isn't the problem in this case. You can have a stateful environment (e.g. a rails applications) that is accessed over the stateless HTTP, but is, itself, stateful.
Tyler Smith
+1  A: 

SESSIONS are good, i use them to hold object state in some of my PHP programming.

Or a better solution would be to use Flex so you don't have to worry about the stateless HTTP protocol...

OOP_Master
Can you tell me more about Flex?
Partial
Adobe Flex is basically similar in concept to true RIA frameworks like Silverlight and JavaFX (i haven't programmed in these though, so i can't give an opinion on them). Flex apps are written in AS3 (for the logic - same language as Flash) and MXML (for the interface). The end result is compiled as a SWF file that automatically generates the HTML to be view in a browser. They can also be run on the desktop using Air. It's also open source, so anyone can program in it. It's just more of a complete package than javascript or jQuery. I've used both and jQuery is not good for lots of data!
OOP_Master