SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: Error: Call to a member function
-
Jun 13, 2007, 11:59 #1
- Join Date
- Mar 2007
- Posts
- 196
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Error: Call to a member function
I recently wrote a session class to store sessions in my database and here it is:
PHP Code:<?php
/**
* Project: Kayzio Framework
* File: Session/main.class.php
*
* @copyright 2007 Kayzio. All Rights Reserved.
* @author Matthew Dahl <dahl.matthew@gmail.com>
* @package Kayzio Framework
*/
/***********************************************************************************/
class SessionManager {
/**
* Session Life Time
*
* @public int
*/
var $LifeTime;
/***********************************************************************************/
/**
* Close
*/
function Close() {
return true;
}
/**
* Destroy
*
* @param int
*/
function Destroy($ID) {
if(strtolower($DB->dbType) == "mysql") {
$NewID = mysql_real_escape_string($ID);
}
else {
$NewID = addslashes($ID);
}
$SessionData = $DB->Query("DELETE FROM sessions WHERE id = '$NewID'");
return true;
}
/**
* Garbage Collection
*/
function GarbageCollection() {
$SessionData = $DB->Query("DELETE FROM sessions WHERE expires < time()");
return true;
}
/**
* Session Manager
*/
function SessionManager() {
$this->LifeTime = get_cfg_var("session.gc_maxlifetime");
session_set_save_handler(
array(&$this, "Open"),
array(&$this, "Close"),
array(&$this, "Read"),
array(&$this, "Write"),
array(&$this, "Destroy"),
array(&$this, "GarbageCollection")
);
}
/**
* Open
*
* @params string, string
*/
function Open($SavePath, $SessionName) {
global $sess_save_path;
$sess_save_path = $SavePath;
return true;
}
/**
* Read
*
* @param int
*/
function Read($ID) {
$Data = "";
$Time = time();
if(strtolower($DB->dbType) == "mysql") {
$NewID = mysql_real_escape_string($ID);
}
else {
$NewID = addslashes($ID);
}
$SessionData = $DB->Query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");
$NumRows = $DB->NumRows($SessionData);
if ($NumRows > 0) {
$Row = $DB->FetchAssoc($SessionData);
$Data = $Row['data'];
}
return $Data;
}
/**
* Write
*
* @params int, string
*/
function Write($ID, $Data) {
$Time = time() + $this->LifeTime;
if(strtolower($DB->dbType) == "mysql") {
$NewID = mysql_real_escape_string($ID);
$NewData = mysql_real_escape_string($Data);
}
else {
$NewID = addslashes($ID);
$NewData = addslashes($Data);
}
$ReplaceSession = $DB->Query("UPDATE sessions SET id = '$NewID' AND data = '$NewData' AND expires = '$Time'");
return true;
}
}
/***********************************************************************************/
?>Code:Fatal error: Call to a member function Query() on a non-object in ... on line 96
PHP Code:$SessionData = $DB->Query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");
MattKayzio - We don't hesitate, we accelerate.
-
Jun 13, 2007, 12:15 #2
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
where have you defined $DB...?
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Jun 13, 2007, 12:18 #3
- Join Date
- Oct 2006
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I guess you will need to place a "global $DB;" at the top of your class
-
Jun 13, 2007, 12:41 #4
- Join Date
- Aug 2005
- Posts
- 453
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How about did you instantiate the object?
$db = new Database();
Where does this db class come from?Computers and Fire ...
In the hands of the inexperienced or uneducated,
the results can be disastrous.
While the professional can tame, master even conquer.
-
Jun 13, 2007, 12:50 #5
- Join Date
- Mar 2007
- Posts
- 196
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In my main file I define DB and I do $sess = new SessionManager(); start_session();. I know the DB class works because I use it all throughout my site. I have used Query, FetchArray, etc. and they all work which is why I am confused. So basically my main file includes all of the classes so I would think DB should work within the session class. How do you define a global in a class b/c if I put global $DB; in my class I get the following error
Code:Parse error: parse error, unexpected T_GLOBAL, expecting T_FUNCTION
Kayzio - We don't hesitate, we accelerate.
-
Jun 13, 2007, 13:33 #6
- Join Date
- Mar 2007
- Posts
- 196
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is really weird, I took the session functions out of a class so that way DB would already be a global and I still get the same error for some reason about call to a member function. And in case your wondering I removed the arrays in the save handler so that they call the proper functions.
PHP Code:<?php
/**
* Project: Kayzio Framework
* File: Session/main.php
*
* @copyright 2007 Kayzio. All Rights Reserved.
* @author Matthew Dahl <dahl.matthew@gmail.com>
* @package Kayzio Framework
*/
/***********************************************************************************/
/**
* Session Life Time
*/
$SessionLifeTime = null;
/***********************************************************************************/
/**
* Close
*/
function SessionClose() {
return true;
}
/**
* Destroy
*
* @param int
*/
function SessionDestroy($ID) {
if(strtolower($DB->dbType) == "mysql") {
$NewID = mysql_real_escape_string($ID);
}
else {
$NewID = addslashes($ID);
}
$SessionData = $DB->Query("DELETE FROM sessions WHERE id = '$NewID'");
return true;
}
/**
* Garbage Collection
*/
function SessionGarbageCollection() {
$SessionData = $DB->Query("DELETE FROM sessions WHERE expires < time()");
return true;
}
/**
* Session Manager
*/
function SessionManager() {
$SessionLifeTime = get_cfg_var("session.gc_maxlifetime");
session_set_save_handler(
"SessionOpen",
"SessionClose",
"SessionRead",
"SessionWrite",
"SessionDestroy",
"SessionGarbageCollection"
);
}
/**
* Open
*
* @params string, string
*/
function SessionOpen($SavePath, $SessionName) {
global $sess_save_path;
$sess_save_path = $SavePath;
return true;
}
/**
* Read
*
* @param int
*/
function SessionRead($ID) {
$Data = "";
$Time = time();
if(strtolower($DB->dbType) == "mysql") {
$NewID = mysql_real_escape_string($ID);
}
else {
$NewID = addslashes($ID);
}
$SessionData = $DB->Query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");
$NumRows = $DB->NumRows($SessionData);
if ($NumRows > 0) {
$Row = $DB->FetchAssoc($SessionData);
$Data = $Row['data'];
}
return $Data;
}
/**
* Write
*
* @params int, string
*/
function SessionWrite($ID, $Data) {
$Time = time() + $this->LifeTime;
if(strtolower($DB->dbType) == "mysql") {
$NewID = mysql_real_escape_string($ID);
$NewData = mysql_real_escape_string($Data);
}
else {
$NewID = addslashes($ID);
$NewData = addslashes($Data);
}
$ReplaceSession = $DB->Query("UPDATE sessions SET id = '$NewID' AND data = '$NewData' AND expires = '$Time'");
return true;
}
/***********************************************************************************/
?>Kayzio - We don't hesitate, we accelerate.
Bookmarks