views:

96

answers:

4

What is best practice with regards to using session variables?

Is it best to just refer to them as session variables or is it better at the beginning of the script to transfer them to local variables of the same name?

I am also a little stumpped on the best folder/file structure for my application if anyone has a useful link with regards to that it would be very useful.. thanks.

+5  A: 

Just access them as they are, there will be no performance hit.

In my mind data is usually in session for a reason, so moving it from the session to local, and the having to put it back again just provides a step for errors to occur, plus it may make your code more confusing to read.

You probably only want to assign the session value to a local variable if you need to manipulate the data and want to retain the original value.

ILMV
+1, that's basically what i would have answered
ChristopheD
Fellow VBForums user. Nice to see you here :)
Pino
Hello Pino, good to see you here too :-)
ILMV
you are right - it only leaves room for errors - thanks
Mark
A: 

Is it best to just refer to them as session variables or is it better at the beginning of the script to transfer them to local variables of the same name?

For me it depends on what you are doing with it, if you are using it once then use $_Session[] if you are doing lots of logic with it, it makes sense to transfer it to a local var.

Either way its preferance.

Pino
+2  A: 

I usually transfer them to local variables if I don't intend to manipulate them, just to avoid the chance of unintentionally overwriting. Plus, it's easier to work with local variables than writing out $_SESSION[''] every time.

Greg
A: 

I'd recommend against using $_SESSION. Use a Session wrapper/manager class for handling session variables.
There are many available out there, but Zend_Session is among the best.

Marius Burz