Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts

    Session Wrapper Class?

    I've been developing in PHP for a long time now, but never really looked too closely into sessions. I can create login scripts and whatever, then set session variables and use those throughout, but I'm just not that confident because I've never felt like I learned the right way to use them.

    I'm looking for any tips about using sessions in general (or tutorials), as well as information on using a session wrapper class. I've seen a few but I'm unsure as to whether or not I really need them or should be using one.

    Thanks!

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    I'm not sure what you mean by a "session wrapper class".

    Certainly though, I use wrapper classes to hide all the superglobals.

    Strictly speaking, it is bad coding practice to use $_SESSION directly in a script.

    I tend to use a singleton class to access $_SESSION.

    Look up "php singleton" and think about how you would like to use that to wrap up $_SESSION. (there are various solutions, its best to chose the one that you prefer).

    Using a wrapper class like that can be very useful for portability, avoiding race hazards in multi-threaded applications, etc, etc.
    Die Welt ist ein Irrenhaus und hier ist die Zentrale!

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    When I say "session wrapper", I mean something like this:

    http://snipplr.com/view/12/php-session-wrapper-class/

    Then, instead of accessing $_SESSION, you instantiate an instance of the Session class to do all of your session handling. The example I provided is a simple one, but there are Session wrappers out there that do a whole lot more crap - notably giving you some sort of additional control over how long the session will last as well as checking to see if the session is expired every time session_start() is called.

    I do see that a singleton (or single instance of the class) would be useful - it doesn't make sense to create a bunch of Session objects that all do the same thing. Good object-oriented design says one is enough and to use that one throughout.

    Do you recommend a wrapper class with the added functionality, or just a simple wrapper that extends the default session functionality to me through a static object?

    Oh, and while we're on the topic, is there an advantage to using a singleton over just making it global? Right now I'm doing that with a database class...no singleton needed, yet I keep just one copy of that class. After all, the end effect is really the same.
    Last edited by bacterozoid; 08-28-2009 at 02:48 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •