php - 如何停止在子类中调用基类的构造函数

标签 php oop

下面是我的基类,即数据库方法。

// 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/

相关文章:

javascript - 从 Angular 到 Laravel 的 POST 不起作用

php - 使用 paypal 的付款信息

java - 如何使用 "equals"为泛型实现 "instanceof"方法?

php - 为什么在 CodeIgniter 中不鼓励在数组中传递数据?

php - 为什么这样做? (php 点符号)

php - 此查询/数据库不能很好地协同工作

php - 当通过 ajax 调用页面时,jQuery 不会第二次运行

javascript - 创建一个新的JS对象实例的最有效方法

oop - 我可以限制 Python3 中的对象,以便只允许我为其设置 setter 的属性吗?

java - 如何为简单的学校应用程序设计对象