storing classes in sessions

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
dolphin713
Forum Newbie
Posts: 10
Joined: Wed Jun 21, 2006 4:26 am

storing classes in sessions

Post by dolphin713 »

Hi,
I have being developing some php pages but always with functions.
Now i think it´s time to move forward and start using classes. I saw that I can save class instances in session variables so I can use it when i load a diferent page. The reason I want to do this is for not having to pass values with POST or GET.
Is this the correct way to achieve this ?
I mean if I instantiate a class in one page, when i load a new page the class values will be lost, right ?
Thanks, and please excuse this newbie questions :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I mean if I instantiate a class in one page, when i load a new page the class values will be lost, right ?
Try the following:
class.inc.php

Code: Select all

class SomeClass {
   var $q;
   var $e;
   function SomeClass($q, $e) {
      $this->q = $q;
      $this->e = $e;
   }
}
page1.php

Code: Select all

require_once 'class.inc.php';
session_start();
$_SESSION['obj'] = new SomeClass('asd', 'sdf');
echo '<a href="page2.php">Click here</a>';
page2.php

Code: Select all

require_once 'class.inc.php';
session_start();
var_dump($_SESSION['obj']);
dolphin713
Forum Newbie
Posts: 10
Joined: Wed Jun 21, 2006 4:26 am

Post by dolphin713 »

I didn´t try it because i think I know what will happen.
You have 2 session_start() so the value from the first session will be lost , right ?
But if you only had the first session probably it would work...i Think
I know how to do it, the question that has been boring me is if this is a good procedure or not, and if there is a better solution.
thanks for the quick reply :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You have 2 session_start() so the value from the first session will be lost , right ?
No, you're wrong. I have 1 session_start() per file, and this is how it's supposed to work (and actually does).
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

I didn´t try it because i think I know what will happen.
I'd try it anyway if I were you... :wink:

It should work fine.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

If i were you I'd also get familiar with __sleep, __wakeup, and __autoload functions
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

bgzee wrote:If i were you I'd also get familiar with __sleep, __wakeup, and __autoload functions
...if you're running php5+.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

__sleep and __wakeup are used in php4 as well.
dolphin713
Forum Newbie
Posts: 10
Joined: Wed Jun 21, 2006 4:26 am

Post by dolphin713 »

Thanks,
i´ve tested it and it worked.
Is this a good procedure or you don´t advice at all doing this ?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

As long, as you keep in session only tiny objects with simple structure you should be ok. I wouldn't advise to store large amount of data in the session anyway.

Side note: it's impossible to serialize variable (or object members) of type resource (e.g. database connection handle). But even this limitation could be circumvented if the object has __wakeup method defined.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post by bg »

Say we are dealing with a large object(s). How does the overhead of serialization at the end of a script and unserializing once the script is accessed again compare to always creating a new instance of an object and populating it each time the script is accessed? Of course, as you said, resource variables will always need to be recreated, but that is generally negligable.

Personally, I can see a lot of upsides to storing objects in the session. First is, not having to deal with the session variable outside of storing your object. Data will always be organized neatly within your objects, without the need to be constantly repopulated each time the script is accessed. As a result of not having to constantly repopulate, you can potentially cut down on database queries.

Of course, I'd like to hear the argument against this. What are the downsides?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Of course, I'd like to hear the argument against this. What are the downsides?
Scalability and performance issues. Imagine the situation:
your service became popular, so popular the 1 web-server couldn't handle it (yet 1 db server is enough). You bought two additional boxes and set them as additional webservers. As a simpliest form of load balancing you chose round-robin dns. Now, since the session state must be mantained throughout the farm, you have to implement centralized session storage. Natural choice would be to move session data to the database. And now, creating a new instance of an object is less expensive process compared to re-creation of an object from session (because you have to deserialize it).

With any caching technique (and what you've proposed is merely a cache) arise the question: when to invalidate? Another mental experiment:
you use session as an intermediate storage in your multi-page wizard-like form. You accumulate a lot of data, but hesitate to store it permanently before the wizard is completed. Yet a user chose not to complete the form and navigated to another part of your site! That part uses session for its own purposes. Now every page of that part serialize and deserialize the data it doesn't need (session garbage collector will not touch that particular session because it still active).
dolphin713
Forum Newbie
Posts: 10
Joined: Wed Jun 21, 2006 4:26 am

Post by dolphin713 »

thanks :)
Post Reply