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

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

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

    点赞 打赏 评论
  • duanduo0520 2013-12-30 12:29

    You shouldn't use inheritance for has-a relationship, inheritance describes the is-a relationship. And in your case Categories is not a Database, Categories has a database.

    Use composition instead:

    class Categories
    {
        private $database;
    
        function __construct(Database $database)
        {
            $this->database = $database;
        }
    
        public function ViewCategories()
        {
            return $this->database->Run("SELECT * FROM categories");
        }
    }
    

    And the usage:

    $connection = new Database("localhost", "raheel", "raheel786", "ecommerce");
    $connection->Connect();
    // ...
    $category = new Categories($connection);
    $category_list = $category->ViewCategories();
    var_dump($category_list);
    
    点赞 打赏 评论

相关推荐 更多相似问题