0

我创建了一个类,将我的类扩展到 PHP 中的 mysqli 类,看到这些错误我真的很震惊

Warning: Missing argument 1 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

Warning: Missing argument 2 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

Warning: Missing argument 3 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

Warning: Missing argument 4 for BeatBeast_Database::__construct(), called in C:\xampp\htdocs\beatbeast\login.php on line 11 and defined in C:\xampp\htdocs\beatbeast\includes\Db\BeatBeast_Db.php on line 6

这是 BeatBeast_Db.php

<?php
    class BeatBeast_Database extends mysqli
    {

        protected $r = 'Something';
        public function __construct($db_host,$db_username,$db_password,$db_name)
        {
            parent::__construct($db_host,$db_username,$db_password,$db_name);

            if(mysqli_connect_error())
            {
                die('Connect Error (' . mysqli_connect_errno() . ')' . mysqli_connect_error());
            }
        }

        public function close()
        {
            $this->close();
        }

    }

    require_once("db_constants.inc.php");
    $conn = new BeatBeast_Database("localhost", "root", "myPass", "beatbeast");

这是我的 login.php

<?php require_once("./includes/Utilities.php") ;?>
<?php require_once("./includes/Db/DatabaseUtilities.php"); ?>
<?php require_once("./includes/Db/Accounts.php");?>
<?php require_once("./includes/Db/BeatBeast_Db.php"); ?>

<?php 
    if(isset($_POST['submit'])){
        require_once("./includes/process_form.inc.php");

        $hashedPass = crypt($password,$username);
        $accounts = new Accounts();
        $accounts->showMessage();

第 11 行是

$accounts = new Accounts();

如果你们有兴趣,这里是我的 Accounts 课程

require_once("BeatBeast_Db.php");

Class Accounts extends BeatBeast_Database
{
    private $accnt_id;
    private $username;
    private $email;

    function info()
    {

        echo "{$this->accnt_id} {$this->username} {$this->email}";
    }

    public static function getIdByUsername($username)
    {
        global $conn;
        $sql = "SELECT accnt_id FROM accounts WHERE username = '{$username}'";
        $rs = $conn->query($sql);
        $found = $rs->fetch_array();
        return $found;
    }

    public function showMessage(){
        echo "{$this->r}";
    }

    public static function getUsernameById($id)
    {
        global $conn;
            $sql = "SELECT username FROM accounts WHERE accnt_id = $id ";
        $rs = $conn->query($sql);
        $found = $rs->fetch_array();
        return $found;
    }

    public function getAccntId()
    {
        return $this->accnt_id;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getEmail()
    {
        return $this->email;
    }
}
4

2 回答 2

1

如果您查看 MySQLi 的 PHP 参考(http://www.php.net/manual/en/class.mysqli.php),原始类构造函数包含 6 个参数:

__construct ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

扩展 MySQLi 时,您必须定义所有这些,即使您不使用。但是,这不是导致警告错误的原因。

在您的情况下,您在调用$accounts = new Accounts();时没有定义任何参数。虽然,Accounts类范围BeatBeast_Database。因此,您需要传递所有BeatBeast_Database构造函数参数 ( $db_host,$db_username,$db_password,$db_name)。

于 2012-06-27T01:23:19.553 回答
0

让我们从 login.php 第 11 行开始

$accounts = new Accounts();

这调用了Accounts类的构造函数——没有定义。自从

Class Accounts extends BeatBeast_Database

PHP 回退到类的构造函数BeatBeast_Database

public function __construct($db_host,$db_username,$db_password,$db_name)

它需要 4 个参数,但没有得到 - 正是错误消息中的内容。您需要将第 11 行更改login.php

$accounts = new Accounts(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);

或者但是您定义的常量db_constants.inc.php已被清除。

于 2012-06-27T01:34:18.670 回答