SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    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;
            }

        }

        
    /***********************************************************************************/

    ?>
    The thing is when I go to use it I get the following error:
    Code:
    Fatal error: Call to a member function Query() on a non-object in ... on line 96
    Here is line 96:
    PHP Code:
                $SessionData $DB->Query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time"); 
    Any ideas to what is actually causing this error and how to fix it. Thanks in advance,
    Matt
    Kayzio - We don't hesitate, we accelerate.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    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.....

  3. #3
    SitePoint Enthusiast
    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

  4. #4
    SitePoint Evangelist
    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.

  5. #5
    SitePoint Zealot
    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.

  6. #6
    SitePoint Zealot
    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

Posting Permissions

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