PDO捕获PDOException错误


PDO catch PDOException Error

如果连接有效,它工作得很好,但如果连接失败,PDOException似乎实际上不起作用。catch完全执行失败。如果失败,则中断$this->dbh = new PDO("$db_driver:host=$db_host;dbname=$db_name", $db_user, $db_pass);这一行的执行。所以我不能在catch块中得到PDOException。我怎么才能让它继续工作呢?

<?php
namespace app'Helpers;
use 'PDO;
/**
* Core class which exists only once through the application
*
*/
class Core
{
public $dbh; // handle of the db connexion
private static $instance;
// constructor to create a MySQLi instance (="MySQL Improved Extension")
private function __construct()
{
    $db_host = ConfigHelper::read('db.host');
    $db_name = ConfigHelper::read('db.basename');
    $db_user = ConfigHelper::read('db.user');
    $db_pass = ConfigHelper::read('db.password');
    $db_driver = ConfigHelper::read('db.driver');
    try {
        $this->dbh = new PDO("$db_driver:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    } catch (PDOException $pdoex) {
        exit("Database connection failed: " . $pdoex->getMessage());
        return false;
    }
}
/**
 * get instance of Core object
 *
 * @return Object self
 */
public static function getInstance()
{
    if (!isset(self::$instance)) {
        $object = __CLASS__;
        self::$instance = new $object;
    }
    return self::$instance;
}
}

这是一个命名空间问题。

当在一个命名空间内时,你必须用反斜杠来处理每个根级的类名。

但你没有。

但是最糟糕的部分是根本不应该捕获PDOException 。让它向全站范围的报告处理程序报告

相关文章: