我创建了一个类,将我的类扩展到 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");
这是我的登录名.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();
如果你们有兴趣,这里是我的会计课
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;
}
}
如果你看一下MySQLi(http://www.php.net/manual/en/class.mysqli.php)的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
)。
让我们从 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 个参数,但没有得到任何参数 - 正是错误消息中的内容。您需要将login.php
的第 11 行更改为
$accounts = new Accounts(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
或者,您在db_constants.inc.php
中定义的常量将被保留。