Php 无法在会话类中创建类实例
我试图在数据库中记录会话。我创建了一个类,并试图在其构造中调用数据库类。 我得到这个错误: 警告:缺少SafePDO::_构造的参数1,在 这是我的数据库类:Php 无法在会话类中创建类实例,php,class,session,Php,Class,Session,我试图在数据库中记录会话。我创建了一个类,并试图在其构造中调用数据库类。 我得到这个错误: 警告:缺少SafePDO::_构造的参数1,在 这是我的数据库类: Class SafePDO extends PDO { public static function exception_handler($exception) { // Output the exception details die('Uncaught exception: ' . $exce
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);
这是我的课程:
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()
}
class Session
{
private $db;
public function __construct($db)
{
$this->db = $db;
这两门课都来自互联网上的真实例子。SavePDO来自PHP.net,会话类来自:
我整天都在试。我的错在哪里?
谢谢大家! 您忘了在会话类中添加dsn:
$this->db = new SafePDO;
由于您的构造没有dsn公共函数_构造$dsn的默认值,因此会引发错误。这是正常的
您应该将此dsn提供给会话类,以便能够创建SafePDO isntance
我不知道您是如何使用它们的,但您应该运行第一个脚本,然后在创建会话时执行类似操作:
$session = new Session($db);
然后在会话类中:
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()
}
class Session
{
private $db;
public function __construct($db)
{
$this->db = $db;
您在没有任何参数的情况下构建了SafePDO类的对象:
$this->db = new SafePDO;
但$dsn参数是必需的
你必须设置它。
您应该设置您的数据库用户名
请参见此示例:
$this->db = new SafePDO('mysql:host=localhost;dbname=example_db;', 'example_usr');
谢谢大家!我想我有一些进步,但还不确定。现在我看到:致命错误:在第48行的/home/mynshost/public_html/tarrot/db_sessions.inc.php中对非对象调用成员函数prepare致命错误:在第35行的/home/mynshost/public_html/tarrot/db_sessions.inc.php中调用未定义的方法SafePDO::close in/home/mynshost/public_html/tarrot/db_sessions.inc