Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 9th, 2003, 03:48 PM
Registered User
 
Join Date: Jun 2003
Location: Houston, Texas, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why is it bad to put Objects into Session Vars?

As the complexity of the tasks I'm trying to tackle has grown I have become increasingly impressed with object oriented design techniques.

I would like to apply these to my php code. My question is twofold:

1. If it is taboo to put objects into session vars, how DO you take advantage of php's OO capabilities w/o using sessions?;

and
2. WHY is it taboo? All I can find on discussion boards are statements such as, "as everyone knows, it is bad form to put objects into session vars...". I have run some tests with a simple example and it is definitely possible to access an object across several pages with sessions.

I am trying to use an application object which will act as a state machine for the application as a whole and control view compositions (assemble php pages from other objects). I really don't see how to take advantage of OO efficiently if the HTTP/statelessness issue is going to force us to re-instantiate and re-set every data member every time a user navigates to a new page. Yet, it seems like MS is doing just this in .NET with the viewstate. I understand that several years ago there were some problems related to ASP 3.0 and IIS, but these would seem to be irrelevant to php and the current state of session technology.

If I cannot stick an object into sessions, can I do it with a reference to an object?

Nik posted something in response to one of my posts several months ago referring to the use of a state machine to control user navigation through my site. Nik, was this an object-based solution?

Any hints, suggestions or examples would be greatly appreciated.

Thanks,


---------------
Trey Carroll
 
Old August 11th, 2003, 06:22 PM
Registered User
 
Join Date: Jun 2003
Location: Houston, Texas, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess my question was too vague or complex. Nik must be on vacation or something.

Anyway, I did some research and found out that php5 is about to address OO in a major way. I guess I'll have to wait to see how the design patterns get implemented when we all get our "new toy". I still don't see how we're going to be able to take advantage of all of the new java-like stuff if we can't stick objects into session vars! What are we going to do put a reference to our objects into a hidden? (ha ha) It seems to me like the whole thing *hinges* on being able to keep application level variables in memory over the course of an individual visit to a site.

If anyone is out there, illuminate me.



---------------
Trey Carroll
 
Old August 13th, 2003, 09:35 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok quick reply,

You most definatley can use objects in session variables, it is not as bad as people make out.

The problem is you must declare your class definition to the php engine before you call 'session_start();'

Just becareful to turn off global_sessions in the php.ini file as this will automatically call 'session_start();' before it processes your code and will fail when it tries to register your session object as a variable. This is the reason why it is often considered taboo to use objects inside sessions as your scripts will not be as portable as they might.
 
Old August 18th, 2003, 12:44 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How funny, I was on vacation. Just got back from 10 days in the Grand Teton and Yellowstone National Parks. It's nice to be missed!

On to business: The reason people have had problems storing objects in sessions is because an object needs to be serialized (converted to a text string) to be stored across pages, and unserialized back into an object when the session is restarted in the next script.

That means (as p15ed mentioned briefly) that PHP needs to KNOW what your object is before it can unserialize your session data, which happens when you call session_start(). That means you need to perform all your relevant includes and class definitions BEFORE your session is started.


Take care,

Nik
http://www.bigaction.org/




Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert New Row from Session Vars iceandrews VB Databases Basics 1 May 12th, 2008 08:16 AM
Security: Could someone falsificate session vars? Maxxim ASP.NET 2.0 Professional 5 February 13th, 2008 12:06 PM
Storing objects in Session variables mega ASP.NET 1.0 and 1.1 Basics 8 November 16th, 2004 02:38 PM
Pass session vars from C# to VB.Net khamutari ASP.NET 1.0 and 1.1 Basics 5 November 11th, 2004 11:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.