Session variable reference in PHP

Suppose I store an array or object named $_SESSION["logged_in_user"] .

If I need to reference it several times throughout the script, which of the following is โ€œbest practiceโ€?

  • Use $_SESSION["logged_in_user"] every time (ie $_SESSION["logged_in_user"]["first_name"] )?

  • Copy an object to a new variable of type $logged_in = $_SESSION["logged_in_user"] ?

  • Create a link to the session variable as $logged_in =& $_SESSION["logged_in_user"]

I probably think too much about this, but my main problems are the script overhead and readability. I'm not sure if referencing a session variable is several times slower than referencing a built-in variable. I also do not know if copying a session variable to the "regular" variable adds more overhead than is necessary. I like the readability of $logged_in["first_name"] over $_SESSION["logged_in_user"]["first_name"] .

So, is there a best practice here, or does it really not matter?

+7
source share
5 answers

You donโ€™t think too much about it ... thinking is good!

I personally use symfony 1.4 for coding that solves such problems, but you can opt out of lean :-)

I would think OO (Object Oriented). Create a user class. Then make calls such as UserFactory :: getCurrentUser () or User :: getCurrentUser (), which will return the currently registered user object. Among the member variables of this class will be user_id. However, you can add add functionality and data to this class.

Remember that thinking in OO means using abstract terms that are very close to a problem area. User, car, order, etc. You do not have to be specific, and you do not need to include all the available information in this class so that it is "complete", whatever that is. Include only the data you need at the moment (hold yagni ). Classes are illusions of concrete, real things. Just like 3D modeling is an illusion of the real world.

Hope this helps ...

+6
source

$_SESSION is a special PHP superglobal array. So you can technically refer to it whenever you want, just using it:

 $_SESSION['logged_in_user'] 

However, this is special because it can change. The following example makes this visible:

 $alias =& $_SESSION; session_start(); $alias['foo'] = 'bar'; 

This code does not set $_SESSION['foo'] . $alias points to the previous $_SESSION , session_start() created a new session.

If you know such caveats, you can create your own $_SESSION abstraction.

+7
source

You donโ€™t think too much about it, in my opinion.

But if you don't have the session wrapper / manager class, I would just use:

 $_SESSION['logged_in_user'] 

In any case, this is not a huge deal. Use everything that works for you.

+1
source

You can create a class to control the session:

 class MySession { public static &get( $key ){ if( !array_key_exists( $key, $_SESSION ){ return null; } return $_SESSION[$key]; } } 

Thus, you do not need to worry about how to retrieve the values, you are not duplicating any data:

 $first_name = MySession::get( 'first_name' ); 

Note. This decision is about access to the session, not your specific implementation :)

+1
source
 $_SESSION["logged_in_user"]["first_name"] 

Stay simple; and do not add overheads to create new rooms for the same variables.

+1
source

All Articles