Welcome to WebmasterWorld Guest from 22.214.171.124
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 :-)
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.
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.
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.
Note: It is not possible to serialize PHP built-in objects.
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!