下面是我的基类,即数据库方法。
// Constructor
public function __construct($argHost, $argUsername, $argPassword, $argDatabase)
{
$this->_host = $argHost;
$this->_username = $argUsername;
$this->_password = $argPassword;
$this->_database = $argDatabase;
}
// Connect to the database
public function Connect()
{
if (!$this->Is_Connected()) {
$this->_connection = mysqli_connect($this->_host,$this->_username,$this->_password,$this->_database);
} else {
return $this->_connection;
}
}
// Run query
public function Run($query)
{
if ($this->result = mysqli_query($this->_connection,$query)) {
return $this->result;
} else {
die("Couldn't perform the request");
}
}
我的子类是下面的类别方法
class Categories extends Database
{
public $category_id = '';
public $category_name = '';
public $category_image = '';
// View Category
public function ViewCategories()
{
return $this->Run("SELECT * FROM categories");
}
}
现在的问题是,当我通过创建基类的对象运行 Run() 方法时,它工作正常。但是当我创建对象对象时,子类即类别并执行方法 viewCategories();我收到以下错误
Warning: Missing argument 1 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17
Warning: Missing argument 2 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17
Warning: Missing argument 3 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17
Warning: Missing argument 4 for Database::__construct(), called in E:\xampplite\htdocs\ecommerce\index.php on line 16 and defined in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 17
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in E:\xampplite\htdocs\ecommerce\classes\class.database.php on line 35 Couldn't perform the request
已更新:这就是我调用方法的方式
<?php
function __autoload($class_name) {
include 'classes/class.'.$class_name . '.php';
}
$connection = new Database("localhost", "raheel", "raheel786", "ecommerce");
$connection->Connect();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ecommerce</title>
</head>
<body>
<?php
$category = new Categories();
$category_list = $category->ViewCategories();
var_dump($category_list);
?>
</body>
</html>
请帮我解决这个问题。
最佳答案
你不应该为 has-a relationship 使用继承,继承描述了is-a relationship .在您的情况下,类别不是数据库,类别有数据库。
改用组合:
class Categories
{
private $database;
function __construct(Database $database)
{
$this->database = $database;
}
public function ViewCategories()
{
return $this->database->Run("SELECT * FROM categories");
}
}
以及用法:
$connection = new Database("localhost", "raheel", "raheel786", "ecommerce");
$connection->Connect();
// ...
$category = new Categories($connection);
$category_list = $category->ViewCategories();
var_dump($category_list);
关于php - 如何停止在子类中调用基类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20839130/