zac1987
zac1987

Reputation: 2777

Call to a member function prepare() on a non-objec on php class and function

<?php
    try{
     $conn = new PDO("mysql:host=$DB_SERVER;dbname=$DB_NAME",$DB_USER,$DB_PASS);
    }

class SessionManager {

        var $life_time;
        function SessionManager() {
            global $conn;
            $this->life_time = get_cfg_var("session.gc_maxlifetime");

            // Register this object as the session handler
            session_set_save_handler( 
                array( &$this, "open" ), 
                array( &$this, "close" ),
                array( &$this, "read" ),
                array( &$this, "write" ),
                array( &$this, "destroy"),
                array( &$this, "gc" )
            );
        }



        function read( $id ) {
            global $conn;
            $data = "";
            $time = time();
            $newid = $id;       
            $sql = "SELECT session_data FROM session_tmp WHERE session_id=? AND expired_date > ?";
            $q = $conn->prepare($sql);
            $result = $q->execute(array($newid, $time));
            while($r = $q->fetch(PDO::FETCH_ASSOC)){
                $data = $row['session_data'];
            }
            return $data;
        }


        function write( $id, $data ) {            
            $time = time() + $this->life_time;
            global $conn;
            $newid = $id;
            $newdata = $data;
            $sql = "SELECT session_id FROM session_tmp WHERE session_id = ?"; // error happen here!!!!
            $q = $conn->prepare($sql);
            $result = $q->execute(array($newid));
            return TRUE;
        }
}

I add global $conn; onto function read() and it fix the error. I don't know why global $conn; cannot fix the error on function write(). How to fix the error?

Upvotes: 0

Views: 216

Answers (1)

SteAp
SteAp

Reputation: 11999

In PHP, a variable like $conn is available in a function if and only if it was defined inside the function. The word 'defined inside of' means 'assigned inside of'.

In your case, $conn is not defined in the write() function, but outside of all functions. Therefore, you need to tell write() that it should refer to the global variable $conn. This is done using

   global $conn;

Note, that use of global is strongly bad code style! You should never need to use global.

Instead of global, you might do something like this:

// This class should actually be a singleton class
// http://en.wikipedia.org/wiki/Singleton_pattern
//
class CDBSession {

  protected 
    $conn;

  public function __construct( $DB_SERVER, $DB_NAME, $DB_USER,$DB_PASS ) {

      $this->conn 
         = new PDO("mysql:host=$DB_SERVER;dbname=$DB_NAME",$DB_USER,$DB_PASS);

  } // __construct

  public function getPDO() {

      return $this->conn;

  }      

}

class SessionManager {

  protected 
    $_PDOSession;

  public function __construct( CDBSession $theDBSession ) {

      $this->_PDOSession = $theDBSession;

  } // __construct


  ... other methods need to access $this->_PDOSession->getPDO() too ...

} // class SessionManager

Finally, you have to use SessionManager like this:

$mySessionManager 
  = new SessionManager(new CDBSession($DB_SERVER, $DB_NAME, $DB_USER,$DB_PASS));

Upvotes: 1

Related Questions