3

这是我的 PDO 连接类:

class DB {

private static $objInstance;

private static $dsn;
private static $user;
private static $pwd;


/*
 * Class Constructor - Create a new database connection if one doesn't exist
* Set to private so no-one can create a new instance via ' = new DB();'
*/
private function __construct() {}

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

private function getConfig()
{
    $config = new Config();
    $db = $config->getConfig('dbcxn');
    DB::$dsn = $db['dsn'];
    DB::$user = $db['user'];
    DB::$pwd = $db['password'];
}

/*
 * Returns DB instance or create initial connection
* @param
* @return $objInstance;
*/
public static function getInstance(  ) {

    if(!self::$objInstance){
        self::getConfig();
        self::$objInstance = new \PDO(DB::$dsn, DB::$user, DB::$pwd);
        self::$objInstance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        self::$objInstance->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    }

    return self::$objInstance;

} # end method

/*
* Passes on any static calls to this class onto the singleton PDO instance
* @param $chrMethod, $arrArguments
* @return $mix
*/
final public static function __callStatic( $chrMethod, $arrArguments ) {

$objInstance = self::getInstance();

    return call_user_func_array(array($objInstance, $chrMethod), $arrArguments);

} # end method
}

我从http://php.net/manual/en/book.pdo.php获取了上面的内容,对我自己的修改很少,但是当我调用类时,我得到了这个错误:

Fatal error: Undefined class constant 'FETCH_ASSOC'...

谁能告诉我如何解决这个问题?

4

1 回答 1

-1

因为你没有 Config() 类....

这里:

$config = new Config();

你需要先上那门课!

编辑:您需要定义 Config 类!

喜欢:

class Config
{
 // construct here
}
于 2013-10-09T11:19:27.240 回答