Jed R. Brubaker wrote:
> Consider the following: I have a login class that is instantiated at
> the top of every page. It can log you in, check to see if you are
> logged in, etc. This class has an assortment of class variables such
> as userID, userType, etc.
[...]
> A solution is to set all of these variables into $_SESSION, but the
> appeal of classes is that I might be able to maintain all of my
> information in a related location, and not in the session.

One possible solution: use a factory method to instantiate your object, and have
this factory method return a cached session object if it exists, or a brand new one
if it doesn't.  This allows you to abstract the session from the way you use the
object, so if you decide to put this data somewhere else later you only need to
alter your factory method.  Example:

function &factory() {

  @session_start();

  /* Create new Login object and cache it if one doesn't already exist */
  if (!isset($_SESSION['LoginObj'])) {
    $_SESSION['LoginObj'] = new Login();
  }

  return $_SESSION['LoginObj'];

}

Using the above approach you could instantiate your object like so:

$login =& Login::factory();

Then your pages wouldn't have to concern themselves with whether or not the object
was cached, or where the data is cached.  I've used the above approach with PHP4
several times.  If you're using PHP5 you can drop the "&" notation as objects are
passed by reference by default.

I hope I understood what you were asking for...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to