错误信息:
致命错误:不在第51行的class.db.php中的对象上下文中使用$ this
错误行:
return $this->PDOInstance->prepare($sql, $driver_options);
代码:
class DB {
public $error = true;
private $PDOInstance = null;
private static $instance = null;
private function __construct()
{
try {
$this->PDOInstance = new PDO('mysql:host='.HOST.';dbname='.DBNAME.';',
USER,
PASSWORD,
array(
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
));
$this->PDOInstance->query("SET NAMES 'cp1251'");
}
catch(PDOException $e) {
echo "error";
exit();
}
}
public static function getInstance()
{
if(is_null(self::$instance))
{
self::$instance = new DB();
}
return self::$instance;
}
private function __clone() {
}
private function __wakeup() {
}
public static function prepare($sql, $driver_options=array())
{
try {
return $this->PDOInstance->prepare($sql, $driver_options); /// ERROR in this line
}
catch(PDOException $e) {
$this->error($e->getMessage());
}
}
}
您正在使用$this
静态函数。$this
引用一个您没有的实例,该实例调用静态函数,因此发生错误。我不明白为什么在单例类中需要非静态属性,但是如果您坚持要拥有它们,这就是您可以做的
catch(PDOException $e) {
self::$instance->error = $e->getMessage();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句