简体   繁体   English

如何在PHP类中使用与DB的连接?

[英]How to use connection to DB inside a PHP Class?

So I'm writing this website that requires me to make User class where i need to fetch some data from DB but when i try $this->conn = new PDO(....) it throws me fatal error as following 所以我正在写这个网站,要求我做User类,在那里我需要从数据库中获取一些数据,但是当我尝试$this->conn = new PDO(....)它会引发致命错误,如下所示

Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in [no active file]:0 Stack trace: #0 [internal function]: PDO->__sleep() #1 {main} thrown in [no active file] on line 0 致命错误:[无活动文件]:0中未捕获异常'PDOException',消息为“您无法序列化或反序列化PDO实例”:0堆栈跟踪:#0 [内部函数]:PDO-> __ sleep()#1 {main}被抛出第0行上的[无活动文件]

Class User: 班级用户:

private $idu = -1;
private $display_name = "";
private $permissions = [];
private $conn = "";

/**
 * User constructor.
 */

function __construct($idu)
{
    $this->conn = new PDO("mysql:host=localhost;dbname=lai","lai","lai");
    $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->idu = $idu;
    $this->setDisplayName($idu);
}

//GETS DISPLAY NAME FROM DB FOR CURRENT USER
private function setDisplayName($idu){
    //$sql = "SELECT `display_name` FROM `users` WHERE `idu`='".$idu."'";
    //$q = $this->conn->prepare($sql);
    //$q->execute();
    //$result = $q->fetch(PDO::FETCH_ASSOC);

    //return $result['display_name'];
}

//GETS PERMISSIONS FROM DB FOR CURRENT USER
private function setPermissions($idu){

}

//RETURNS ID OF CURRENT USER
public function getIDU(){
    return $this->idu;
}

//RETURNS DISPLAY NAME FOR CURRENT NAME
public function getDisplayName(){
    return $this->display_name;
}

//RETURNS PERMISSIONS FOR CURRENT USER
public function getPermissions(){
    return $this->permissions;
}

I don't need to store connection inside the class, but need to be able to use DB in my class, I've also tried declaring 我不需要在类内部存储连接,但是需要能够在我的类中使用数据库,我也尝试过声明

$conn = new PDO(....);

outside of the class, globally and it also didn't work 在课堂以外,在全球范围内也没有用

You store that instance of user somewhere, like in the session? 您将用户实例存储在某个地方,例如在会话中吗?

PHP wants to serialize that class somewhere in your code, and all of it's field, even the $conn field. PHP希望在您的代码中的任何地方序列化该类,并且所有字段都是该字段,甚至$ conn字段也是如此。 In order to store the whole user instance in the session, then you should either: 为了将整个用户实例存储在会话中,您应该:

  1. remove the reference to the db (which is considered a good idea) and move it to some repository or mapper class to retreive and manipulate user objects. 删除对db的引用(这被认为是一个好主意),并将其移至某个存储库或mapper类以检索和操作用户对象。
  2. write a __wakeup method inside your User class which returns an array of the properties to be serialized: 在User类中编写__wakeup方法,该方法返回要序列化的属性的数组:

    public function __wakeup() { return ["idu", "display_name", "permissions"]; 公共功能__wakeup(){return [“ idu”,“ display_name”,“ permissions”]; // no $conn here! //这里没有$ conn! } This way it wont try to serialize the connection resource. 这样,它将不会尝试序列化连接资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM