I have the following classes in PHP Database.class.php
<?php
abstract class Database {
private $dataBaseHandler;
private $error;
private $statement;
protected function __construct() {
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dataBaseHandler = new PDO('mysql:host='. DB_HOST.';dbname='. DB_NAME, DB_USER, DB_PASS, $options);
$this->dataBaseHandler->exec('SET NAMES UTF8');
}
catch(PDOException $e) {
$this->error = $e->getMessage();
}
}
protected function query($query) {
$this->statement = $this->dataBaseHandler->prepare($query);
}
protected function bind($param, $value, $type = null) {
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->statement->bindValue($param, $value, $type);
}
protected function execute() {
return $this->statement->execute();
}
protected function resultset() {
$this->execute();
return $this->statement->fetchAll(PDO::FETCH_ASSOC);
}
protected function single() {
$this->execute();
return $this->statement->fetch(PDO::FETCH_ASSOC);
}
protected function rowCount() {
return $this->statement->rowCount();
}
protected function lastInsertId() {
return $this->dataBaseHandler->lastInsertId();
}
protected function beginTransaction() {
return $this->dataBaseHandler->beginTransaction();
}
protected function endTransaction() {
return $this->dataBaseHandler->commit();
}
protected function cancelTransaction() {
return $this->dataBaseHandler->rollBack();
}
protected function debugDumpParams() {
return $this->statement->debugDumpParams();
}
protected function closeCursor() {
$this->statement->closeCursor();
}
}?>
and the ControllerRegister.class
<?php
include_once 'classes/Database.class.php';
class ControllerRegister extends Database {
private $firstName, $lastName, $email, $password, $confPassword;
public function __construct() {
$this->firstName = filter_input(INPUT_POST,'firstname',FILTER_SANITIZE_STRING);
$this->lastName = filter_input(INPUT_POST,'lastname',FILTER_SANITIZE_STRING);
$this->email = filter_input(INPUT_POST,'email',FILTER_SANITIZE_STRING);
$this->password = filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
$this->confPassword = filter_input(INPUT_POST,'confPassword',FILTER_SANITIZE_STRING);
$this->checkPassword();
}
private function checkPassword() {
if($this->password == $this->confPassword)
{
$this->registerPassword();
}else{
echo 'Different password and password confirmation.';
return false;
}
}
private function hashPassword() {
$this->password = password_hash($this->password,PASSWORD_DEFAULT);
}
private function registerPassword() {
// Define configuration
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'exemple');
$this->hashPassword();
$this->query('INSERT INTO users VALUES (NULL, :first_name, :last_name, :email, :password)');
$this->bind(':password', $this->password);
$this->bind(':firstName',$this->firstName);
$this->bind(':lastName', $this->lastName);
$this->bind(':email', $this->email);
$this->bind(':password',$this->password);
$this->execute();
}
}
$new = new ControllerRegister();?>
The data is passed to ControllerRegister
through an html form and when I submit the data I get the error Fatal error: Call to a member function prepare() on null in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\web\settings\classes\Database.class.php on line 23
. I'm a whole morning breaking my head, please help me, Grate!