Welcome to WebmasterWorld Guest from 23.22.79.235

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to 'keep an object alive' in php

persistent(?) objects in php

   
11:34 am on Mar 3, 2009 (gmt 0)

5+ Year Member



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 :-)

hugs
immols

9:46 pm on Mar 3, 2009 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

5+ Year Member



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)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

5+ Year Member



Thanks again.

It's exactly what i am doing at the moment. As you said, soring he objects themselves is too much hassle :D

Regards
immols

3:45 pm on Mar 4, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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)

5+ Year Member



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)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

5+ Year Member



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 :-)

Cheers

I'll try working with serialize today. Exciting!