I'm trying to create a class in PHP that connects to the MySQL database and I'm getting the following error:
Fatal error: Using $ this when not in object context
ConnectDB.class.php
class ConnectDB
{
public $pdo;
private $driver, $host, $port, $base, $user, $pass;
public function __construct() {
$this->setDriver('mysql');
$this->setHost('localhost');
$this->setPort('3306');
$this->setBase('bsn');
$this->setUser('root');
$this->setPass('');
$this->connect();
}
public function connect() {
try {
$this->pdo = new PDO("{$this->getDriver()}:host={$this->getHost()};port={$this->getPort()};dbname={$this->getBase()}", $this->getUser(), $this->getPass());
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
} catch (PDOException $e) {
die('Ocorreu um erro na conexão com o banco de dados.');
}
}
public function setDriver($str) {
$this->driver = $str;
}
public function setHost($str) {
$this->host = $str;
}
public function setPort($str) {
$this->port = $str;
}
public function setBase($str) {
$this->base = $str;
}
public function setUser($str) {
$this->user = $str;
}
public function setPass($str) {
$this->pass = $str;
}
public function getPDO() {
return $this->pdo;
}
public function getDriver() {
return $this->driver;
}
public function getHost() {
return $this->host;
}
public function getPort() {
return $this->port;
}
public function getBase() {
return $this->base;
}
public function getUser() {
return $this->user;
}
public function getPass() {
return $this->pass;
}
}
ManipulateData.class.php
require_once('ConnectDB.class.php');
require_once('ITemplate.php');
class ManipulateData extends ConnectDB implements ITemplate
{
public function __construct() {
parent::__construct();
}
public static function select($query) {
$stmt = parent::getPDO()->prepare($query);
$stmt->execute();
return $stmt->fetchAll();
}
public static function insert($query) {}
public static function update($query) {}
public static function delete($query) {}
public static function doubleData($dados) {}
}
I'm trying to run my queries like this:
print '<pre>';
print_r(ManipulateData::select('SELECT * FROM vagas'));
What am I doing wrong?