views:

84

answers:

2

I was wondering what the best way is for storing and retrieving session values in PHP. I am building a php framework with an MVC structure. Now i have a registry class with inmportant object.

I have a class for the session, with some defined functions concerning the session. Now I was wondering if it is a good practice to just use

$_SESSION['something'] 

everywhere in your code. Or should i use set and get methods in the session object class. If i do so it would look something like

$session->something = 4
$myalue = $session->something

I know this is not a big deal, but i was just wondering what the best practice is for such a thing in a good architecture.

Thank you!

+3  A: 

I tend to side with a Session Object Class mainly because it gives me a proxy between the actual session date and what classes are trying to set.

For instance:

When I have a site live, and a user says they are having problems with X or Y, it is very easy for me to insert a Session Object with data that makes it look like I am that user, without needing to actually log in as that user, or erase any of my own Session data.

The other advantage is that I don't have to worry about how I name my variables. This is especially handy if I need to make sure my application doesn't step on another app's toes. I can simply change my variable names in the Session storage, while keeping them the same name when facing my app.

Chacha102
+1  A: 

If you build a wrapper class around something, you should use that wrapper class. Otherwise what's the point?

The thing you usually want to shoot for is a layer of abstraction. If you ever decide to change the implementation detail of how exactly the session data is stored (like storing data in $_SESSION['Prefix']['something'], or switch the whole thing to a database backend), you can do so by simply changing the details of the session class, while the rest of your code works without changes.

deceze