Php 数据库连接太多

Php 数据库连接太多,php,database,pdo,Php,Database,Pdo,在我的logout.php端点中,我使用了3个不同的类。这3类中的每一类都有: private $conn; private $table_name = "users"; // constructor with $db as database connection public function __construct($db) { $this->conn = $db; } 此代码用于在初始化类时从端点连接到数据库,如下所示: // include needed files i

在我的
logout.php
端点中,我使用了3个不同的类。这3类中的每一类都有:

private $conn;
private $table_name = "users";

// constructor with $db as database connection
public function __construct($db)
{
    $this->conn = $db;
}
此代码用于在初始化类时从端点连接到数据库,如下所示:

// include needed files
include_once '../config/database.php';
include_once '../classes/user.php';
include_once '../classes/token.php';

// instantiate database and product object
$database = new Database();
$db = $database->getConnection();

// initialize object
$user = new user($db);
$token = new token($db);
在某些函数中的这3个类中,我有时需要使用其他类,因此我再次在用户类中包含类似于此注销函数的内容:


我不确定这是否是正确的方法,我正在调试我的代码,并试图对其进行重构以使其更有意义。为了使用令牌类,我是否需要在用户类中的注销函数中再次包含此db?或者我可以使用我在端点中通过
\uu construct
初始化的连接,而不是一次又一次地
包含
数据库?

这是一种糟糕的方法,首先,您应该只在文件顶部包含
一次

不需要在注销方法中再次包含它,通常方法体内部的
包含
在我看来通常是一种代码味道,除非在某些特定情况下

其次,您要建立到数据库的两个多连接,每次实例化数据库类
$database=newdatabase()
并调用
$database->getConnection()
时,您都要建立到数据库的新连接。您应该只实例化数据库一次,然后将其注入需要DB连接的类中(通过构造函数或作为方法参数传递)

最后但并非最不重要的一点,您应该确保
$database->getConnection()

你可以这样做

<?php
    class Database
    {
        // specify your own database credentials
        private $host = "localhost";
        private $db_name = "obiezaca_db";
        private $username = "root";
        private $password = "";
        private $conn;
        // get the database connection

        public function getConnection()
        {
            if (!$this->conn) {
                try {
                    $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
                    $this->conn->exec("set names utf8");
                } catch(PDOException $exception) {
                    // you shouldn't output exception error message in production
                    // because it can leak sensitive data such as DB username and password
                    echo "Database error";
                }
            }  
            return $this->conn;
        }
    }
?>
<?php
    class Database 
    {
        // specify your own database credentials
        private $host = "localhost";
        private $db_name = "obiezaca_db";
        private $username = "root";
        private $password = "";

        private $conn;

        private static $instance;

        private function __construct() {};

        private function __clone() {};

        public static getInstance() 
        {
            if (!static::$instance) {
                static::$instance = new Database(); 
            } 
            return static::$instance;
        }

        // get the database connection
        public function getConnection()
        {
            if (!$this->conn) {
                try {
                    $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
                    $this->conn->exec("set names utf8");
                } catch(PDOException $exception) {
                    // you shouldn't output exception error message in production
                    // because it can leak sensitive data such as DB username and password
                    echo "Database error";
                }
            }  
            return $this->conn;
        }
    }

 $database = Database::getInstance();
 $connection = $database->getConnection();

这是一种糟糕的方法,首先,您应该只在文件顶部包含一次

不需要在注销方法中再次包含它,通常方法体内部的
包含
在我看来通常是一种代码味道,除非在某些特定情况下

其次,您要建立到数据库的两个多连接,每次实例化数据库类
$database=newdatabase()
并调用
$database->getConnection()
时,您都要建立到数据库的新连接。您应该只实例化数据库一次,然后将其注入需要DB连接的类中(通过构造函数或作为方法参数传递)

最后但并非最不重要的一点,您应该确保
$database->getConnection()

你可以这样做

<?php
    class Database
    {
        // specify your own database credentials
        private $host = "localhost";
        private $db_name = "obiezaca_db";
        private $username = "root";
        private $password = "";
        private $conn;
        // get the database connection

        public function getConnection()
        {
            if (!$this->conn) {
                try {
                    $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
                    $this->conn->exec("set names utf8");
                } catch(PDOException $exception) {
                    // you shouldn't output exception error message in production
                    // because it can leak sensitive data such as DB username and password
                    echo "Database error";
                }
            }  
            return $this->conn;
        }
    }
?>
<?php
    class Database 
    {
        // specify your own database credentials
        private $host = "localhost";
        private $db_name = "obiezaca_db";
        private $username = "root";
        private $password = "";

        private $conn;

        private static $instance;

        private function __construct() {};

        private function __clone() {};

        public static getInstance() 
        {
            if (!static::$instance) {
                static::$instance = new Database(); 
            } 
            return static::$instance;
        }

        // get the database connection
        public function getConnection()
        {
            if (!$this->conn) {
                try {
                    $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
                    $this->conn->exec("set names utf8");
                } catch(PDOException $exception) {
                    // you shouldn't output exception error message in production
                    // because it can leak sensitive data such as DB username and password
                    echo "Database error";
                }
            }  
            return $this->conn;
        }
    }

 $database = Database::getInstance();
 $connection = $database->getConnection();

你可以使用DI框架和类自动加载器你可以使用DI框架和类自动加载器谢谢你的回复这对我很有帮助。有了这个构造函数,我现在可以像这样初始化我的类:
$token=newtoken($this->conn)
,现在我可以删除
include database
,因为我实际上使用的是constructor。在类声明之前,我将
include-token
移动到了文件的顶部。现在好点了吗?@BT101你需要像这样传递它们
$token=newtoken($db->getConnection())
,因为
$conn
是一个私有属性,以确保它不能从类外更改。当private conn设置为这样时,
$token=new token($this->conn)
可以正常工作:
公共函数u构造($db){$this->conn=$db;}
其中参数
$db
$db->getConnection()
实际上
$db=$database->getConnection()$用户=新用户($db)感谢您的回复,这对我很有帮助。有了这个构造函数,我现在可以像这样初始化我的类:
$token=newtoken($this->conn)
,现在我可以删除
include database
,因为我实际上使用的是constructor。在类声明之前,我将
include-token
移动到了文件的顶部。现在好点了吗?@BT101你需要像这样传递它们
$token=newtoken($db->getConnection())
,因为
$conn
是一个私有属性,以确保它不能从类外更改。当private conn设置为这样时,
$token=new token($this->conn)
可以正常工作:
公共函数u构造($db){$this->conn=$db;}
其中参数
$db
$db->getConnection()
实际上
$db=$database->getConnection()$用户=新用户($db)