dtsps2098 2013-12-30 12:01
浏览 64
已采纳

如何在子类中调用基类的构造函数

Below is my base class i.e database methods.

// 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");
    }
}

And my child class is categories method below

class Categories extends Database
{    
    public $category_id = '';
    public $category_name = '';
    public $category_image = '';

    // View Category
    public function ViewCategories() 
    {
        return $this->Run("SELECT * FROM categories");
    }       
}

Now the problem is that when i am running the Run() method by creating object of the base class it is working fine. But when i am creating object object the child class i.e categories and executing the method viewCategories(); i m receiving below errors

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

Udated : This is how i am calling the methods

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

Kindly help me how to fix that.

  • 写回答

2条回答 默认 最新

  • duanchongzi9997 2013-12-30 12:10
    关注

    In your case you need to overload constructor. Just add empty __construct() method to child class

    class Categories extends Database{
    
        public function __construct() {}
    
        ...
    
    }
    

    but it will not solve your problem:)

    As a solution i can see 2 variants:

    1) make static variable _connection so it will be available in all objects

    public function Connect(){
        if( ! self::$_connection){
            self::$_connection = mysqli_connect($this->_host,$this->_username,$this->_password,$this->_database);    
        }
    
        return self::$_connection;
    }
    
    public function Run($query){
        if($this->result = mysqli_query(self::$_connection,$query)){
        return $this->result;
                    }
                    else 
                       die("Couldn't perform the request");
    }
    

    2) I think it is better way. Make 2 independent classes with connect and queries so one will contain another

    class Database {
        // 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{
        private $db;
    
        public function __construct(Database $db) {
            $this->db = $db;
        }
    
        public $category_id = '';
        public $category_name = '';
        public $category_image = '';
    
        // View Category
        public function ViewCategories() {
            return $this->db->Run("SELECT * FROM categories");
        }   
    }
    

    So usage is:

    $connection = new Database("localhost", "raheel", "raheel786", "ecommerce");
    $connection->Connect();
    
    $category = new Categories($connection);
    

    Attention! I didn't test it, just give an example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析