Error message:
Fatal error: Using $this when
not in object context in class.db.php on line - 51
Error line:
return $this->PDOInstance->prepare($sql, $driver_options);
Code:
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());
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…