如何在__construct()中创建PDO连接?

时间:2015-10-21 11:50:00

标签: php mysql pdo

我有一个包含三个方法的类。所有这些方法都需要数据库,然后我想在一切之前创建一个连接数据库的系统。像这样:

Class myclass
{
    private $db;

    public function __construct() {

        $db = new PDO("mysql:host = hostname; dbname = database",username, password);
    }

    function one()   {/* it needs to database and I will use it like this: */
                         $this->$db->prepare("select ...");
                     }
    function two()   {/* also it needs to database */}
    function three() {/* also it needs to database */}
}

现在我想知道(首先)我做的是标准方法?和(在第二个)我如何检查[如果连接断开(在其他字连接不存在)然后连接]?

3 个答案:

答案 0 :(得分:1)

2种可能的方法:

1。:扩展PDO类(您还将继承所有公共方法)

这是我个人的最爱。

Class myclass extends PDO
{
    //constructor is inherited from PDO so you dont need to create a new one

    function one()   {/* it needs to database and I will use it like this: */
                         //access internal methods via $this:
                         $this->prepare("select ...");
                     }
    function two()   {/* also it needs to database */}
    function three() {/* also it needs to database */}
}

尝试建立连接:

try{
    $db = new myclass("mysql:host = hostname; dbname = database","useer", "asdf");
}
 catch (PDOException $e) {
   print "Error!: " . $e->getMessage() . "<br/>".PHP_EOL;
   exit("ERROR OCCURED");
}

2。将pdo对象创建为属性

类myclass2 {     私人$ db;

public function __construct() {

    try{
            $this->db = new myclass("mysql:host = hostname; dbname = database","useer", "asdf");
            }
             catch (PDOException $e) {
               print "Error!: " . $e->getMessage() . "<br/>".PHP_EOL;
               exit("ERROR OCCURED");
            }
    }

function one()   {/* it needs to database and I will use it like this: */
                     $this->db->prepare("select ...");
                 }
function two()   {/* also it needs to database */}
function three() {/* also it needs to database */}

}

答案 1 :(得分:0)

创建类db

class db {

/*** Declare instance ***/
private static $instance = NULL;

/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
private function __construct() {
  /*** maybe set the db name here later ***/
}

/**
*
* Return DB instance or create intitial connection
*
* @return object (PDO)
*
* @access public
*
*/
public static function getInstance() {
    if (!self::$instance) {
        self::$instance = new PDO("mysql:host=".DB_HOSTNAME.";dbname=".DB_DATABASE, DB_USERNAME, DB_PASSWORD);
        self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
    }

    return self::$instance;
}

public static function close() {
    if (self::$instance) {
        self::$instance = null;
    }
}

/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/
private function __clone(){
}

} /*** end of class ***/

使用它:

DB::getInstance()
哇,答案很差。好的:))

对我来说似乎是最好的方式。只需创建一个类,并在需要时调用它的实例。

答案 2 :(得分:0)

要访问类属性,必须使用->语法:

Class myclass
{
    private $db;

    public function __construct() {

        $this->db = new PDO("mysql:host = hostname; dbname = database",username, password);
    }

    function one()   {/* it needs to database and I will use it like this: */
        $this->db->prepare("select ...");
    }
    function two()   {/* also it needs to database */}
    function three() {/* also it needs to database */}
}

如果要检测错误,请使用

启用错误信号
$this->db->setAttributePDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

并在使用DB的代码周围使用try/catch

function one() {
    try {
        $this->db->prepare(...);
    } catch (PDOException $e) {
        // handle error
    }
}