Fatal error: Call to a Member Function prepare() on null in

Asked

Viewed 692 times

0

I have the following PHP classes 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 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 the ControllerRegister through an html form and when submit the data is returned me 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 cracking my head, please help me, thank you!

  • 1

    If you call __construct in ControllerRegister, the __construct of Database will never run, which implies that the connection to the bank will not be made. You know what inheritance is between classes?

  • then I have to create a method to make the connection? I thought the __construct would also perform in the child classes

  • If you overwrite, don’t - in this case you need to call it explicitly.

  • um, and how would I override this?

  • If trouble in the prepare() your connection is void or had another type of failure, check that the PDO object has been created correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.