|How to 'keep an object alive' in php|
persistent(?) objects in php
| 11:34 am on Mar 3, 2009 (gmt 0)|
Trying to make a website using classes. Having fun at it, but now i am stuck again. Could use some help in changing the way i think :-)
I have an index.php that makes the default page of the website: headers, menu, footers, etc. It includes (require_once) engine.php. (Well, it IS the engine that makes the website go :P)
I have a couple of class files that are included in engine.php (require_once). The Page class (page.class.php) generates the webpage. It talks to instances of html.class.php and to databaseobject.class.php. These class-files are also included in engine.php. Also, instances of these classes are created in engine.php.
Now my problem: each time i reload my website, i get new instances of each class:
$dbo = new DataBaseObject();
I do not want that, bcs at the moment i am loosing an array of image-objects out of which i need to pass data to my databaseobject.
I thought about putting the objects into the session, but i guess that's stupid.
I thought about only making the objects if they are not already there, but that seems to always be the case.
So my question is: can i refer to my objects after a pageload (Not sure if persistent is the right word here)? Or is that not possible in this case.
If i can, how should i do that? Where should i check? A link or a tip is highly appreciated. Or an example, ofc :-)
| 9:46 pm on Mar 3, 2009 (gmt 0)|
HTTP is a stateless protocol. Once a page is sent, the server is done with it.
If you want to maintain persistence between pageloads, you have to do so using the session, cookies, or your database.
| 7:34 am on Mar 4, 2009 (gmt 0)|
Thank you for your reply, Gregg.
I think i am nearing the limits of what to do with scripting languages? Or is PHP more restricted in this sense than other languages are?
Is it considered 'good practice' to use classes in php and store the objects in a session? Or should i start using another language? Do i have other options within php? (I am familiar with php but not with another language (although i have done a tiny bit of perl)).
Thanks for hits, suggestions, answers.
| 3:10 pm on Mar 4, 2009 (gmt 0)|
I'm pretty sure you can't store an object instantiation in a session. Even if you could, storing too much data in the session can slow everything down.
All web based applications are stateless and find ways to deal with that while maintaining persistence between requests. Many large sites are powered by PHP and manage just fine. It just takes a little bit of training your mind to accept that each page request is *technically* not connected to the last. Then you have to figure out a way to connect them.
Typically, you would assign a unique identifier to each user and store their relevant data in a database or in a session. Then you use that *data* on each page load to populate your objects. You don't actually try to carry the objects themselves as they take up too much memory.
| 3:22 pm on Mar 4, 2009 (gmt 0)|
It's exactly what i am doing at the moment. As you said, soring he objects themselves is too much hassle :D
| 3:45 pm on Mar 4, 2009 (gmt 0)|
|I'm pretty sure you can't store an object instantiation in a session. |
You can store objects in sessions if you serialize() [uk.php.net] them first (ie. convert the object instance into a string representation). And unserialize() [uk.php.net] it to turn it back into your object instance.
PHP Manual: Serializing objects - objects in sessions [uk.php.net]
| 3:58 pm on Mar 4, 2009 (gmt 0)|
So it's possible to put a complete object in a session (or a database) before pageload and get it out of there again after pageload?
Interesting. I need to think about this before i start using it. Thanks for the remark, penders.
| 7:30 pm on Mar 4, 2009 (gmt 0)|
OOP is one area of the PHP manual where the docs are terrible. If you are using PHP5 you tend to jump right over the OOP for PHP4 documentation and miss some important information. Also, keep in mind that you cannot serialize PHP built-in objects. It is an obscure note, but exists, and can be found on the serialize [php.net] manual page:
|Note: It is not possible to serialize PHP built-in objects. |
| 7:26 am on Mar 5, 2009 (gmt 0)|
Hey again coopster!
Yeah, i saw that one. Thanks for mentioning. I assume i do not need to serialize built-in objects anyway. I mean: if they are built-in, the structure is known anyways, and i can just store the data in some array if needed, right?
Also, i will go to php4 manuals before gettting frustrated, i promise :-)
I'll try working with serialize today. Exciting!