2012-02-26 9 views
0

私のセッションクラス取得:phpのセッションクラス - エラー

<?php 

include_once('db.php'); 

class PDOSession 
{ 
    protected $pdo; 
    protected $table = 'SessionData'; 

    public function __construct() 
    { 
     // Get a database connection 
     $db = new PDOConnectionFactory(); 
     $this->pdo = $db->getConnection(true); 

     // Start session 
     session_set_save_handler(array(__CLASS__, '_open'), 
           array(__CLASS__, '_close'), 
           array(__CLASS__, '_read'), 
           array(__CLASS__, '_write'), 
           array(__CLASS__, '_destroy'), 
           array(__CLASS__, '_gc')); 
     session_start(); 
    } 

    public function __destruct() 
    { 
     session_write_close(); 
    } 

    protected function fetchSession($id) 
    { 
     $stmt = $this->pdo->prepare('SELECT id, data FROM '.$this->table.' WHERE id = :id AND unixtime > :unixtime'); 
     $stmt->execute(array(':id' => $id, ':unixtime' => (time() - (int)ini_get('session.gc_maxlifetime')))); 
     $sessions = $stmt->fetchAll(); 

     return empty($sessions) ? false : $sessions[0]; 
    } 

    protected function _open($savePath, $sessionName) 
    { 
     return true; 
    } 

    protected function _close() 
    { 
     return true; 
    } 

    protected function _read($id) 
    { 
     $session = $this->fetchSession($id); 
     return ($session === false) ? false : $session['data']; 
    } 

    protected function _write($id, $sessionData) 
    { 
     $session = $this->fetchSession($id); 
     if($session === false) { 
      $stmt = $this->pdo->prepare('INSERT INTO '.$this->table.' (id, data, unixtime) VALUES (:id, :data, :time)'); 
     } else { 
      $stmt = $this->pdo->prepare('UPDATE '.$this->table.' SET data = :data, unixtime = :time WHERE id = :id'); 
     } 
     $stmt->execute(array(
         ':id' => $id, 
         ':data' => $sessionData, 
         ':time' => time() 
         )); 
    } 

    protected function _destroy($id) 
    { 
     $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE id = :id'); 
     $stmt->execute(array(':id' => $id)); 
    } 

    protected function _gc($maxlifetime) 
    { 
     $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE unixtime < :time'); 
     $stmt->execute(array(':time' => (time() - (int) $maxlifetime))); 
    } 
} 
$newPDOSessionStartHere = new PDOSession(); 

マイエラー:

Warning: Invalid callback PDOSession::_destroy, cannot access protected method PDOSession::_destroy() in auth.php on line 49 

Warning: session_destroy() [function.session-destroy]: Session object destruction failed in auth.php on line 49 

Warning: Invalid callback PDOSession::_close, cannot access protected method PDOSession::_close() in auth.php on line 49 

は、なぜ私はエラーを取得していますか?メソッドを公開すると、$thisにアクセスする際にエラーが発生します。

答えて

1
session_set_save_handler(array(__CLASS__, '_open'), 
         array(__CLASS__, '_close'), 
         array(__CLASS__, '_read'), 
         array(__CLASS__, '_write'), 
         array(__CLASS__, '_destroy'), 
         array(__CLASS__, '_gc')); 

これらの登録の方法は、あなたがパブリックとして宣言する必要があると私はあなたがこれらのメソッドは静的are't becouse代わりCLASSスタンスの$ thisキーワードを使用するために持っていると思うので、外部からアクセスできるようにする必要があります。