Jump to content

Persistent Objects via session, good idea or bad?


El Phantom

Recommended Posts

Hey Everyone, first post on this forum. I was about to start a login system for a site and was thinking about making it Object based, specifically I was thinking about creating a Login class that i would pass around via a session and which would contain all data pertaining to the user and the logged-in session. As I understand it however,  i would have to serialize this login object and place it in a database table or a session variable in order to pass it from page to page. Is this a wise idea, or should i just pass the individual login variables around via the session and forget the object oriented approach? does anybody have any experience they would be willing to share, just wondering if the OO approach is wise if I need a persistent object.

 

I will be using PHP5. Thanks in advance to anyone who answers.

 

--EP

You can create an instance of a class directly in a session variable and as long as the class definition exists before the session_start() you can resume that instance of the class -

 

<?php
require 'your_class.php';
session_start();
if(!isset($_SESSION['some_object_name'])){
// create instance
$_SESSION['some_object_name'] = new your_class();
echo 'object created<br />';
} else {
echo 'object exists<br />';
}

$_SESSION['some_object_name']->class_function();
echo $_SESSION['some_object_name']->class_variable;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.