2016-01-13 103 views
-4

我開始做PHP OO(我知道javaOO),但我希望有人能幫我實例化一個變量,以便我可以調用它的方法。Instantiate variable = new Object;

幾個小時前我讀了一篇文章,我會這樣做。如何將對象會話從PHP傳遞到MySQL。

我把所有的課程從GitHub結束。

我使用本教程:http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/

全2類:https://github.com/plagodzinski/dbsession/tree/master/include

我試圖用實例化:

$ObjSession = new Session; // this line get a error and not work 

我錯了呢?

更新13/01/2016 16; 47

這是我的錯誤:

Warning Call to a member function prepare() on a non-object 

堂課

class Session 
{ 
    private $db; 

    public function __construct() { 

     // instantiate the new database object 
     $this->db = new Database(); 

     // set handler to override session 
     session_set_save_handler(
      array($this, "_open"), 
      array($this, "_close"), 
      array($this, "_read"), 
      array($this, "_write"), 
      array($this, "_destroy"), 
      array($this, "_gc") 
     ); 

     // start the session 
     session_start(); 
    } 

    // check if the database connection is up 
    public function _open() { 
     if ($this->db) { 
      return true; 
     } 

     return false; 
    } 

    // close the database connection 
    public function _close() { 
     if ($this->db->close()) { 
      return true; 
     } 

     return false; 
    } 

    // read session values 
    public function _read($id) { 
     $this->db->query('SELECT data FROM sessions WHERE id = :id'); 

     if ($this->db->execute(array($id))) { 
      $row = $this->db->single(); 
      return $row->data; 
     } else { 
      return ''; 
     } 
    } 

    // write session values 
    public function _write($id, $data) { 
     $access = time(); 
     $this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)'); 

     if ($this->db->execute(array($id, $access, $data))) { 
      return true; 
     } 

     return false; 
    } 

    // destroy session 
    public function _destroy($id) { 
     $this->db->query('DELETE FROM sessions WHERE id = :id'); 
     if ($this->db->execute(array($id))) { 
      return true; 
     } 

     return false; 
    } 

    // garbage collection 
    public function _gc($max) { 
     $old = time() - $max; 

     $this->db->query('DELETE * FROM sessions WHERE access < :old'); 

     if ($this->db->execute(array($old))) { 
      return true; 
     } 

     return false; 
    } 
} 

級數據庫

define("DB_HOST", ""); 
define("DB_NAME", ""); 
define("DB_USER", ""); 
define("DB_PASS", ""); 

class Database 
{ 
    private $host = DB_HOST; 
    private $user = DB_USER; 
    private $pass = DB_PASS; 
    private $dbname = DB_NAME; 
    private $dbh; 
    private $error; 
    private $stmt; 
    public function __construct() { 
     // set DSN 
     $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; 
     // set OPTIONS 
     $options = array(
       PDO::ATTR_PERSISTENT => true, 
       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
     ); 
     // create a new PDO instance or catch any errors 
     try { 
      $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); 
     } catch (PDOException $e) { 
      $this->error = $e->getMessage(); 
     } 
    } 

    // prepare statement 
    public function query($query) { 
     $this->stmt = $this->dbh->prepare($query); 
    } 

    // bind values and execute statement 
    public function execute(array $params = null) { 
     $this->stmt->execute($params); 
    } 

    // fetch single row result 
    public function single() { 
     try { 
      return $this->stmt->fetch(PDO::FETCH_OBJ); 
     } catch (PDOException $e) { 
      $this->error = $e->getMessage(); 
     } 
    } 

    // fetch all results 
    public function resultset() { 
     try { 
      return $this->stmt->fetchAll(PDO::FETCH_OBJ); 
     } catch (PDOException $e) { 
      $this->error = $e->getMessage(); 
     } 
    } 

    // fetch output parameter from stored procedure 
    public function outParam($paramName, $paramAsName) { 
     $this->stmt->closeCursor(); 
     return $this->dbh->query('SELECT ' . $paramName . ' AS ' . $paramAsName)->fetch(PDO::FETCH_OBJ); 
    } 

    // get affected rows count 
    public function rowCount() { 
     return $this->stmt->rowCount(); 
    } 

    // get the id of the last inserted row 
    public function lastInsertId() { 
     return $this->dbh->lastInsertId(); 
    } 

    // begin transaction 
    public function beginTransaction() { 
     return $this->dbh->beginTransaction(); 
    } 

    // end transaction 
    public function endTransaction() { 
     return $this->dbh->commit(); 
    } 

    // cancel transaction 
    public function cancelTransaction() { 
     return $this->dbh->rollBack(); 
    } 

    // debug dump parameters 
    public function debugDumpParams() { 
     return $this->stmt->debugDumpParams(); 
    } 

    // close connection 
    public function close() { 
     $this->dbh = null; 
    } 
} 
+0

我真的不考慮它,但你不只是單純地忘記了perenthesis? '新的Classname()'。 (錯誤報告(E_ALL); ini_set('display_errors',1);'並嘗試阻止使用錯誤保持'@') – Xyv

+0

發生了什麼錯誤你得到?開放的php標籤應該是'<?php',而不是'<?'。 –

+0

不知道爲什麼你選擇不顯示錯誤...但是在你的代碼中兩次你似乎成功實例化新對象。 (在這個頁面上找一個「新」來查看它們)。那麼究竟是什麼問題呢? – David

回答

0

對不起慢了,我自己開心地做其他的事情:

解決方案:

I get errors in the same file I do not know very well (best import)

include('database.class.php'); 
include('session.class.php'); 

Instantiate

$session = new Session(); 

Add query/execute (Database)

PDOException $e 

Important all arrays need a token(:) to recognize ":variable" =>$variable

$this->db->execute(array($id))$this->db->execute(array(":id" =>$id))

畢竟步驟,你可以保存會話到mysql

相關問題