2013-10-09 34 views
2

这里是我的PDO连接类:致命错误:未定义类常量“FETCH_ASSOC”,同时设置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'...

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

+0

而是所有尝试加入'使用PDO的反斜杠;'后您的命名空间声明,看看是否仍然抛出的错误 - 看不出什么毛病的代码,所以它只是一个真的在 – naththedeveloper

+0

拍摄,谢谢FDL,解决PDO类未找到问题,但未定义的类常量错误仍然存​​在。我添加了const FETCH_ASSOC = 2;并且错误停止了,但我仍然觉得有什么问题 –

+0

您确定安装了PDO吗? :) –

回答

-1

becouse你没有配置()类....

这里:

$config = new Config(); 

你需要做的是第一类!

编辑: 您需要定义配置类!

,如:

class Config 
{ 
// construct here 
} 
相关问题