【问题标题】:Can't create class instance in Session Class无法在会话类中创建类实例
【发布时间】:2014-02-28 02:53:25
【问题描述】:

我正在尝试在数据库中记录会话。我创建了类,并试图在其构造中调用数据库类。 我收到此错误:

警告:SafePDO::__construct() 缺少参数 1,调用...

这是我的数据库类:

Class SafePDO extends PDO {

    public static function exception_handler($exception) {
        // Output the exception details
        die('Uncaught exception: ' . $exception->getMessage());
    }

    public function __construct($dsn, $username = '', $password = '', $driver_options = array()) {

        // Temporarily change the PHP exception handler while we...
        set_exception_handler(array(__CLASS__, 'exception_handler'));

        // ... create a PDO object
        parent::__construct($dsn, $username, $password, $driver_options);

        // Change the exception handler back to whatever it was before
        restore_exception_handler();
    }

}

$db = new PDO('mysql:host=localhost;dbname=mynshost_tarrot;charset=utf8', 'mynshost_tarrot', 'PickTarrot2');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

这是我的 Session 课程:

class Session {


    private $db;

    public function __construct() {
        // Instantiate new Database object

        $this->db = new SafePDO;

// Set handler to overide 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();
    }

    /**
     * Open
     */
    public function _open() {
        // If successful
        if ($this->db) {
            return true;
        }
        return false;
    }

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

    /**
     * Read
     */
    public function _read($id) {


        try {
            $stmt = $db->prepare("SELECT data FROM sessions WHERE id = :user_id");
            $stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
            $stmt->execute();
            $row_sess_data = $stmt->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $ex) {
            print "<h3>An Error occured!</h3>";
            print $ex->getMessage();
        }

        return $row_sess_data['data'];
    }

    /**
     * Write
     */
    public function _write($user_id, $data) {
        // Create time stamp
        $access = time();

        try {
            $stmt = $db->prepare("REPLACE INTO sessions VALUES (:user_id, :access, :data)");
            $stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
            $stmt->bindValue(':access', $access, PDO::PARAM_STR);
            $stmt->bindValue(':data', $data, PDO::PARAM_STR);
            $stmt->execute();
//            $row_sess_data = $stmt->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $ex) {
            print "<h3>An Error occured!</h3>";
            print $ex->getMessage();
        }
    }

// End of __write()

    /**
     * Destroy
     */
    public function _destroy($id) {

        try {
            $stmt = $db->prepare("DELETE FROM sessions WHERE id = :user_id");
            $stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
            $stmt->execute();
        } catch (PDOException $ex) {
            print "<h3>An Error occured!</h3>";
            print $ex->getMessage();
        }
    }

// End of __destroy()

    /**
     * Garbage Collection
     */
    public function _gc($max) {
        // Calculate what is to be deemed old
        $old = time() - $max;

        try {
            $stmt = $db->prepare("DELETE * FROM sessions WHERE access < :old");
            $stmt->bindValue(':old', $old, PDO::PARAM_STR);
            $stmt->execute();
        } catch (PDOException $ex) {
            print "<h3>An Error occured!</h3>";
            print $ex->getMessage();
        }
    }

    // End of _gc()
}

这两个课程均来自互联网上的真实示例。 SavePDO 来自 PHP.net,Session 类来自这里:http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/

我整天都在尝试。我的错误在哪里? 谢谢!

【问题讨论】:

    标签: php class session


    【解决方案1】:

    您忘记在此处将您的 dsn 添加到您的 Session 类中:

    $this->db = new SafePDO;
    

    由于您的构造没有 dsn public function __construct($dsn, 的默认值,因此会引发错误。这是正常的。

    您应该将此 dsn 提供给您的 Session 类,以便能够创建 SafePDO istance。

    我不知道您是如何使用它们的,但是您应该运行第一个脚本,然后在创建会话时执行类似的操作:

    $session = new Session($db);
    

    然后在你的 Session 类中:

    class Session
    {
        private $db;
    
        public function __construct($db)
        {    
            $this->db = $db;
    

    【讨论】:

    • 谢谢你们!我想我有一些进展,但还不确定。现在我看到了:致命错误:在第 48 行的 /home/mynshost/public_html/tarrot/db_sessions.inc.php 中的非对象上调用成员函数 prepare() 致命错误:调用未定义的方法 SafePDO::close () 在 /home/mynshost/public_html/tarrot/db_sessions.inc.php 第 35 行
    【解决方案2】:

    你在没有任何参数的情况下构建了SafePDO 类的对象:

    $this->db = new SafePDO;
    

    $dsn 参数是必需的

    您必须设置它。 你应该设置你的数据库用户名。

    看这个例子:

    $this->db = new SafePDO('mysql:host=localhost;dbname=example_db;', 'example_usr');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2021-11-28
      • 2013-12-03
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多