【问题标题】:PHP: Call to undefined function error in same class (file)PHP:调用同一类(文件)中的未定义函数错误
【发布时间】:2014-07-22 18:42:42
【问题描述】:

我正在开发一个使用 SQL 的项目。现在我创建了一个检查数据库的函数 (check_database($post))。该函数应该能够在任何其他函数中调用。我在同一个类中的另一个函数 (authenticate($post)) 调用此函数。当我尝试 php 文件时,我收到以下错误:

致命错误:91 行的 /Applications/XAMPP/xamppfiles/htdocs/Bootstrap-Profile/Code/Controllers/AccountController.php 中调用未定义函数 check_database() >

我的完整代码会明确错误在哪里:

<?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();
    }
}

91 行

} else if (check_database($post)) {

我尝试了很多方法,但就是找不到问题所在。希望有人能找到它:)

【问题讨论】:

  • 你不能调用类方法,不管是静态的还是其他的,只能通过它们的名字来调用。您可以使用$this-&gt;methodNameself::methodName。在您的情况下,它是self::methodName
  • @JohnConde 6 年后你救了我的命 :) 用这个明显的注释

标签: php function class


【解决方案1】:

以不同的方式调用您的成员函数。您似乎知道您使用$this 用于“普通”方法(如__destruct() 所示。对于静态成员,您可以参考the class name

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

或者您在引用同一类中的方法时使用self

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

【讨论】:

    【解决方案2】:

    您已经定义了静态的 check_database 函数。因此,您必须使用 self 调用它。

    像这样:

    self::check_database($post)
    

    【讨论】:

    • 不幸的是,这没有奏效。事实上,它给出了一个新错误:Fatal error: Using $this when not in object context in /Applications/XAMPP/xamppfiles/htdocs/Bootstrap-Profile/Code/Controllers/AccountController.php on line 44。第 44 行是if($result = $this-&gt;connection-&gt;query($query)){
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2021-08-30
    相关资源
    最近更新 更多