i have a file that initializes my application environment...let's call it init.php:
require_once('error_handler.php');
require_once('db.php');
$db = new database(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($db->is_not_valid)
$error_handler->error("Could not connect to the database.");
require_once('session_class.php');
$session = new session_class();
This was working fine on PHP 4 but now that i've upgraded to php 5, my session class is throwing an error. Basically my session class was written to use a database to handle sessions. I create the $db object in init.php and then all the session handling methods in the session class refer to it.
the problem i'm having is that my session handler's WRITE function (which runs after all other page code has executed) is complaining that my $db object is not defined. As you can see from my session class code (an approximation of which is below), the WRITE function refers to the $db object as a global var.
How do I work around this? Does my WRITE function have to create a whole new connection to some database? Is there some way to keep my global objects defined even as these cleanup functions run? I remember wrestling over how to keep my classes separate when writing this session class....
class session_class {
function session_class() {
global $log;
ini_set("session.gc_probability", SESSION_GC_PROBABILITY);
ini_set("session.gc_divisor", SESSION_GC_DIVISOR);
session_set_cookie_params(SESSION_COOKIE_LIFETIME,
COOKIE_PATH,
COOKIE_DOMAIN,
COOKIE_SECURE);
session_name(SESSION_NAME);
session_set_save_handler(
array(&$this, '_open'),
array(&$this, '_close'),
array(&$this, '_read'),
array(&$this, '_write'),
array(&$this, '_destroy'),
array(&$this, '_gc')
);
session_start();
} // session_class()
function _write($id, $data) {
global $db;
if (!is_object($db)) {
// THIS IS THE ERROR THAT KEEPS COMING UP
die('Cannot do session write(). Dbase not defined.');
}
// otherwise, WRITE DB HERE
}
function _open($save_path, $session_name) {
// blah blah
}
function _close() {
// blah blah
}
function _read($id) {
// blah blah
}
} // session_class