harm27
harm27

Reputation: 41

PHP: Call to undefined function error in same class (file)

I'm working on a project that uses SQL. Now I have made a function that checks the database (check_database($post)). That function should be able to be called in any other function. My other function (authenticate($post)), in the same class, calls this function. When I try the php file out, I get the following error:

Fatal error: Call to undefined function check_database() in /Applications/XAMPP/xamppfiles/htdocs/Bootstrap-Profile/Code/Controllers/AccountController.php on line 91

My full code will make clear where the error is:

<?php
require_once('ConnectionController.php'); // requires
require_once('LayoutController.php');

class AccountController {
    private $connection;

    function __construct(){
        $server = new ConnectionController();
        $this->connection = $server->getConnection();
    }

    /**
     * Get account.
     * @return array|bool
     */
    function getUser($email){
        $query = "SELECT Naam, Email, Password, AccountStatus FROM Gebruiker WHERE Email=".$email.";";

        if($result = $this->connection->query($query)){
            return $result->fetch_assoc();
        }else{
            return false;
        }
    }

    /**
     * Create random string
     * @param int $length
     * @return string
     */
    function generateRandomString($length) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    static function check_database($post) {
        $query = "SELECT Password, AccountStatus FROM Gebruiker WHERE Email='" .trim($post['email']). "'";

        if($result = $this->connection->query($query)){

            if (password_verify($post['password'], $result['Password'])) {

                if($result['AccountStatus'] == 'Actief') {
                    $_SESSION['email']=trim($post['email']);

                    return true;

                } else {
                    alert("warning", "<b>Foutmelding! </b>Je account is nog niet geactiveerd! Check je mail.");

                    return false;
                }

            } else {
                alert("danger", "<b>Foutmelding! </b>Het e-mailadres en wachtwoord komen niet overheen!");   


                return false;
            }

        } else {

            alert("danger", "<b>Foutmelding! </b>Het opgegeven e-mailadres bestaat niet!");

            return false;
        }
    }

    /**
     * Login the user in.
     * @param $account
     * @return array|bool

     * $email = trim($account['inputLoginEmail']);
     */
    function authenticate($post){
        if (!isset($post['email']) || empty($post['email'])) {
            alert("danger", "<b>Foutmelding! </b>Vul een e-mailadres in!");

        } else if (!filter_var(trim($post['email']), FILTER_VALIDATE_EMAIL)) {
            alert("danger", "<b>Foutmelding! </b>Ongeldig e-mailadres!");

        } else if (!isset($post['password']) || empty($post['password'])) {
            alert("danger", "<b>Foutmelding! </b>Vul een wachtwoord in!");

        } else if (check_database($post)) {
            loadpage('profile.php');
        }
    }

    function __destruct(){
        $this->connection->close();
    }
}

Line 91:

} else if (check_database($post)) {

I tried many things, but I just can't find the issue. Hopefully someone can find it :)

Upvotes: 1

Views: 4816

Answers (2)

user2854865
user2854865

Reputation: 65

You have defined check_database function static. Because of this you have to call it with self.

Like so:

self::check_database($post)

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33491

Call your member function differently. You seem to know that you use $this for "normal" methods (as shown in __destruct(). For static members, you refer to the class name:

if (AccountController::check_database($post)) {
   ...
}

or you use self when referring to methods in the same class:

if (self::check_database($post)) {
   ...
}

Upvotes: 1

Related Questions