实例化变量=新对象;

时间:2016-01-13 15:25:49

标签: php class object

我开始做php OO(我知道javaOO)但我希望有人可以帮我实例化一个变量,以便我可以调用它的方法。

几个小时前我读了一篇文章,我会这样做。如何将对象会话从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

我尝试用instanciate:

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

我错了什么?

Updated 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;
    }
}

1 个答案:

答案 0 :(得分:0)

抱歉延迟,我很自问其他事情:

解决方案:

  

我在同一个文件中收到错误,我不太了解(最好导入)

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

实例化

$session = new Session();
  

添加查询/执行(数据库)

PDOException $e
  

重要的是,所有数组都需要一个令牌(:)来识别&#34;:变量&#34;   =&GT; $变量

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

完成所有步骤后,您可以将会话保存到mysql